输出正确,但我遇到编译错误。

huangapple go评论54阅读模式
英文:

Outputs correctly but I am getting compiler error

问题

以下是您提供的代码的翻译:

一切都按照应该的方式输出但我得到了编译器错误我在 ZyBooks 上做这个您能告诉我我做错了什么吗

import java.util.Scanner;

public class Inputkey {
   public static void main(String[] args) {
      Scanner scnr = new Scanner(System.in);

      String inputString = "";
      int inputInt = 0; 
      
      inputString = scnr.next();
      inputInt = scnr.nextInt();
      
      while ( !inputString.equals("Stop")){
         if ( inputInt <= 35) {
            System.out.println( inputString + ": 需要重新排序");
         }
         inputString = scnr.next();
         inputInt = scnr.nextInt();
         
      }
            
   }
}

我不会回答翻译请求之外的问题。

英文:

Everything is outputting the way it should but I am getting compiler error. I am doing this on ZyBooks. Can you let me know what I am doing wrong?

import java.util.Scanner;

public class Inputkey {
   public static void main(String[] args) {
      Scanner scnr = new Scanner(System.in);

      String inputString = &quot;&quot;;
      int inputInt = 0; 
      
      inputString = scnr.next();
      inputInt = scnr.nextInt();
      
      while ( !inputString.equals(&quot;Stop&quot;)){
         if ( inputInt &lt;= 35) {
            System.out.println( inputString + &quot;: reorder soon&quot;);
         }
         inputString = scnr.next();
         inputInt = scnr.nextInt();
         
      }
            
   }
}

The error I am getting:
Exception in thread "main"

java.util.NoSuchElementException
	at java.base/java.util.Scanner.throwFor(Scanner.java:937)
	at java.base/java.util.Scanner.next(Scanner.java:1594)
	at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
	at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
	at Inputkey.main(Inputkey.java:18)

答案1

得分: 1

替代 inputString = scnr.next(); 使用 inputString = scnr.nextLine();

Java Scanner 的 next() 和 nextLine() 方法的区别在于,nextLine() 返回文本行中直到回车符的每个字符,而 next() 将该行分割成单个单词,逐个返回单个文本字符串。

import java.util.Scanner;

public class Inputkey {
    public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in);

        String inputString = "";
        int inputInt = 0;

        inputString = scnr.nextLine();
        inputInt = Integer.parseInt(scnr.nextLine());

        while (!inputString.equals("Stop")) {
            if (inputInt <= 35) {
                System.out.println(inputString + ": reorder soon");
            }
            inputString = scnr.nextLine();
            inputInt = Integer.parseInt(scnr.nextLine());
        }
    }
}

示例输入:

This is stackoverflow
35
stop
999
Stop

scanner.next() 将返回 This,而 scanner.nextLine() 将返回 This is stackoverflow

英文:

instead of inputString = scnr.next(); use inputString = scnr.nextLine();

Java Scanner’s next() and nextLine() methods is that nextLine() returns every character in a line of text right up until the carriage return, while next() splits the line up into individual words, returning individual text Strings one at a time.

import java.util.Scanner;

public class Inputkey {
    public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in);

        String inputString = &quot;&quot;;
        int inputInt = 0;

        inputString = scnr.nextLine();
        inputInt = Integer.parseInt(scnr.nextLine());

        while ( !inputString.equals(&quot;Stop&quot;)){
            if ( inputInt &lt;= 35) {
                System.out.println( inputString + &quot;: reorder soon&quot;);
            }
            inputString = scnr.nextLine();
            inputInt = Integer.parseInt(scnr.nextLine());
        }
    }
}

Sample Input:

This is stackoverflow
35
stop
999
Stop

scanner.next() will returns This while scanner.nextLine() will returns This is stackoverflow

huangapple
  • 本文由 发表于 2023年6月2日 01:18:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76384282.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定