關于JAVA的IF和循環語句的語法,不懂如何再次輸入
answer=prompt("Would you like to count sheep?");
if(answer == yes)
while(!(count / 50)) //如果count 還沒有開始數,或是50
{ //倍數,循環開始,count 初始為0
answer=prompt("Still can't sleep, would you like to count again?");
if(answer == no)
break;//我用的是C++的語法,我想應該是一樣的
else
while((++count/50))//如果count是50的倍數,循環退出
{
*("Sleep ");
}
}
樓上那樣寫,如果YES后,應該是個無限循環,count在第一次超過50后,永遠再也不會==50了,除非在函數內部定義count.
這個圖形對吧?
*
***
*****
*******
*****
***
*
你確定不是 for/while/do-while loop?if 不是循環語句吧
用for loop做的:
public class NestedLoop{
public static void main (String[] args){
for (int i=0; iif (i %2 == 0){
for (int s=0; *(" ");
}
for (int d=0; *("*");
}
*n();
}
}
for (int i=5; i>-1; i--){
if (i %2 == 0){
for (int s=0; *(" ");
}
for (int d=0; *("*");
}
*n();
}
}
}
}
給點分唄?我時差黨,大半夜12點多跟這給你做題~ ^ ^
java if語句嵌套if語句
import *r;
public class jsq {
//我想寫一個計算器,if這樣嵌套報錯,在c里就可以。我剛學Java,該怎么改?
public static void main(String[] args)
{Scanner s=new Scanner(*);
Scanner sz=new Scanner(*);
Scanner o=new Scanner(*);
int q,w,e = 0;
char p;
*n("請輸入兩個數字后輸入運算符號");
q=*t();
w=*t();
String p1=*();
if (*("+")){
e=q+w;
*n("結果是:"+e);
}
else if(*("-")){
e=q-w;
*n("結果是:"+e);
}
else if(*("/")){
e=q/w;
*n("結果是:"+e);
}
else if(*("*")){
e=q*w;
*n("結果是:"+e);
}
{
}
}
}
//從控制臺讀入 不能讀取char類型的字符可以用String類型 在用equals()函數來比較兩個字符
//串的是否相等
java 循環語句 分別用 if switch while dowhile for 循環求出1到100的和 -
if switch 是條件判斷語句,在這兒得用循環呀!
用for:
int sum=0;
for(int i=1;i{
sum+=i;
}
*n(sum);
用while:
int sum=0;
int i=1;
while(i{
sum=sum+i;
i++;
}
*n(sum);
用 do-while:
int sum=0;
int i=1;
do
{
sum=sum+i;
i++;
}while(*n(sum);
其實在這用哪個循環都一樣!
轉載請注明出處華閱文章網 » javaif循環語句