亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

請問Scanner在輸入回車后,如何才能退出?

package?com.imooc;

import?java.util.Scanner;

/*@kernal
?*?功能描述:
?*?為指定成績加分,直到分數大于等于?60?為止,
?*?輸出加分前和加分后的成績,并統計加分的次數.
?*/
public?class?addScore?{
public?static?void?main(String[]?args)?{

System.out.print("請輸入需要加分的成績:");
Scanner?sc?=?new?Scanner(System.in);
int?score?=?sc.nextInt();
if?(score?>=?60)?{
System.out.print("您的輸入有誤,請重新輸入(小于60的數字):?");
Scanner?sc2?=?new?Scanner(System.in);
int?scoreRtry?=?sc2.nextInt();
System.out.println("重新輸入后需要加分的成績為:"+scoreRtry+"分");
sc2.close();
int?count=0;
for(;scoreRtry<=60;scoreRtry++)
{
while(scoreRtry?<?60)
{
scoreRtry++;
count++;
}
System.out.println("重新輸入后加分的成績為:"+scoreRtry+"分");
System.out.println("重新輸入后加分的次數為:"+count);
}

}else?if?(score?<?60)
{
System.out.println("加分前的成績為:"+score+"分");
sc.close();
int?count=0;
for(;score<=60;score++)
{
while(score?<?60)
{
score++;
count++;
}
System.out.println("加分后的成績為:"+score+"分");
System.out.println("加分的次數為:"+count+"次");
}
}else?{
System.out.println("###########");
}

}

}

老師,你好,這是我寫的代碼,我想要加入一個當Scanner輸入回車后,直接退出并提示“輸入有誤”的功能,毫無頭緒,o(╯□╰)o ,@laurenyang ? O(∩_∩)O謝謝

正在回答

4 回答

雖然不清楚你的具體要求,不過你可以嘗試拋出異常(后面的課程)來實現

0 回復 有任何疑惑可以回復我~

Sorry,沒有看清楚你的具體要求。

我覺得沒有必要吧,如果什么都不輸,直接回車。程序默認還是提示你繼續輸入,如果輸入的不是自定義的int型信息,程序直接報錯的。

0 回復 有任何疑惑可以回復我~

/*

?* 功能描述:

?* 為指定成績加分,直到分數大于等于 60 為止,

?* 輸出加分前和加分后的成績,并統計加分的次數.

?*/

import java.util.Scanner;

?

public class addScore{

public static void main(String[] args){

System.out.println("請輸入需要加分的成績:");

Scanner sc = new Scanner(System.in);

int score = sc.nextInt();

int count=0;

//先循環判斷輸入的成績是否大于等于60,如果大于等于的話,提示用戶重新輸入,直到輸入的成績小于60,退出循環;

while(score>=60){

System.out.println("您的輸入有誤,請重新輸入(小于60的數字): ");

score = sc.nextInt();

if(score<60){

break;//直到輸入的成績小于60,退出循環;

}

}

while (score<60) {

score++;

count++;

}

System.out.println("加分后的成績為"+score);

System.out.println("加分次數為"+count);

}

}


我也是初學者,代碼可能不是很簡潔,但是可以滿足你的要求。


0 回復 有任何疑惑可以回復我~

..直接關閉就可以了

0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

請問Scanner在輸入回車后,如何才能退出?

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號