2017-11-25 148 views
-2

所以我正在做這件事,要求你輸入你想要的數字,然後讓你輸入一個數字。該代碼應該檢查這個數字是否是第一次提交的數字之一,如果是,會彈出一條消息說出來,然後退出該程序。有人可以幫助我如何正確使用掃描儀,或者如果這不是我的代碼中的問題,幫我糾正我的代碼?使用掃描儀需要幫助

package individuellt_val; 

    import java.util.Scanner; 
    import javax.swing.JOptionPane; 


    public class InlKap9test2 { 

     public static void main(String[] args) { 
      String s = JOptionPane.showInputDialog("Ange några tal"); 
      Scanner sc = new Scanner(s); 
      int x = 0, y = 0; 
      boolean fortsätt = true; 

      while(fortsätt){ 
       while(sc.hasNextInt()){ 
        x = sc.nextInt(); 

       String s2 = JOptionPane.showInputDialog("Ange ett tal till"); 
       Scanner sc2 = new Scanner(s2); 
       while(sc2.hasNextInt()) 
       y = sc.nextInt(); 

       if(x == y){ 
        JOptionPane.showMessageDialog(null, "Detta tal fanns bland de första!"); 
        fortsätt = false; 
       } 
       } 
      } 
    System.exit(0); 
     } 

    } 
+1

1)*「我似乎無法讓它工作。」*你能設法提出一個問題嗎? 2)請使用代碼格式設置代碼和代碼片段,結構化文檔(如HTML/XML或輸入/輸出)。爲此,請選擇文本並單擊郵件發佈/編輯表單頂部的「{}」按鈕。 –

+0

使用'新的掃描儀(System.in)' –

回答

1

說明

new Scanner(s)不正確。 s是一個字符串,而Scanner需要PrintStream。你可能想從鍵盤上找到它,又名System.in

如何解決

變化Scanner sc = new Scanner(s);Scanner sc = new Scanner(System.in)

0

String s = JOptionPane.showInputDialog(「Angenågratal」); 掃描儀sc =新掃描儀

在這裏你不能通過掃描儀的參考。 使用 new Scanner(System.in);

它現在將從控制檯,即鍵盤輸入輸入。