❶ 在java中 列印1--100的奇數 每行10個 數字右對齊
for (int i = 1; i < 100; i+=2) {
if(i < 10){
System.out.print(" "+i+"\t");
} else{
System.out.print(i+"\t");
}
if((i+1)%20 == 0){
System.out.println();
}
}
❷ 編寫java程序,分別應用while語句、do-while語句,求出100以內所有奇數的和
whileint i=1;
int result=0;
while(i<=100){
result+=i;
i+=2;
}
System.out.println("100內奇數和為:"+result);
do{}while{}
int i=1;
int result=0;
do {
result+=i;
i+=2;
}while (i<=100);
System.out.println("100內奇數和為:"+result);
❸ 如何用java輸出1到100之間的奇數呢
packagesrc;
publicclassTest{
publicTest(){
//TODOAuto-generatedconstructorstub
}
/**
*@paramargs
*/
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
//輸出偶數
for(inti=1;i<=100;i++){
i++;
System.out.print(i);
System.out.print(" ");
}
//輸出奇數
for(inti=0;i<100;i++){
i++;
System.out.print(i);
System.out.print(" ");
}
}
}