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

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何修復掃描儀以在 Java 中在一段時間內工作?

如何修復掃描儀以在 Java 中在一段時間內工作?

白衣染霜花 2023-05-17 14:38:50
我正在做一個簡單的“算命”程序,只是為了好玩!但是,我似乎無法讓我的掃描儀第二次讀取此人的姓名,我應該關閉掃描儀嗎?如果有,在哪一部分?我嘗試在循環結束時將其關閉repeat==true,但效果不佳。do{    int n = rand.nextInt(50);    int m = rand.nextInt(50);    int o = rand.nextInt(50);     System.out.println("blah blah fortune");    System.out.println("input your name"); // This part doesn't work the second time    String name = scanner.nextLine();    System.out.println();    Prediction(name,n,m,o);    System.out.println();    do{        System.out.println("do you wish to guess another fortune?");        System.out.println("1 is yes other number no");        //this part I will omit tests if the answer is valid and if it should repeat itself, it works for now.    }    while(!valid);}while (repeat==true);現在它跳過了寫名字的選項,只是用一個空格作為名字來算命,但循環在其他方面運行良好。它應該詢問下一個它要讀取其命運的人的名字。
查看完整描述

2 回答

?
jeck貓

TA貢獻1909條經驗 獲得超7個贊

您的代碼看起來很適合與Scanner. 我會再看看你的循環(不清楚內部do...while()循環在做什么)以及你的其他代碼中還有什么(什么是Prediction?你的設置方式是否存在錯誤valid?)。


這是一個非常簡單的循環,Scanner永遠循環,詢問一個名字,打印這個名字:


Scanner scanner = new Scanner(System.in);

do {

    System.out.println("input your name");

    String name = scanner.nextLine();

    System.out.println("name: " + name);

} while (true);


查看完整回答
反對 回復 2023-05-17
?
弒天下

TA貢獻1818條經驗 獲得超8個贊

do{

int n = rand.nextInt(50);

int m = rand.nextInt(50);

int o = rand.nextInt(50); 


System.out.println("blah blah fortune");

System.out.println("input your name"); // This part doesn't work the second time

String name = scanner.next();


System.out.println();

Prediction(name,n,m,o);

System.out.println();


 System.out.println("do you wish to guess another fortune?");

    System.out.println("1 is yes other number no");

    //this part I will omit tests if the answer is valid and if it should repeat 

   itself, it works for now.



 }

 while (repeat==true);

使用 scanner.next() 而不是 nextLine(),nextLine() 將在下次開始時將空行作為輸入,因此使用 next()。這就是為什么它不接受輸入的原因。您可以只用一個 while 循環實現相同的功能,但不確定您的要求是什么。


查看完整回答
反對 回復 2023-05-17
  • 2 回答
  • 0 關注
  • 162 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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