2017-09-11 2 views
0

Ich habe eine Frage. Dieses Programm soll zwei ganze Zahlen R und L (beide zwischen 1 und 1000) erhalten und die Fläche und das Volumen eines Zylinders berechnen. Mein Problem ist, dass ich Laufzeitfehler bekomme. Hier ist mein Code:Java Runtime Error zur Berechnung Fläche und Volumen des Zylinders

import java.util.Scanner; 
public class Main 
{ 

    public static void main(String[] args) 
    { 
     Scanner input = new Scanner(System.in); 
     int radius = input.nextInt(); 
     Scanner input2 = new Scanner(System.in); 
     int length = input2.nextInt(); 

     while ((radius > 1000) || (radius < 1)) 
     { 
      input = new Scanner(System.in); 
      radius = input.nextInt(); 
     } 

     while ((length > 1000) || (length < 1)) 
     { 
      input2 = new Scanner(System.in); 
      length = input2.nextInt(); 
     } 

     double area = (radius * radius) * 3.14159; 
     double volume = area * length; 

     System.out.printf("%.1f\n", area); 
     System.out.printf("%.1f\n", volume); 
    } 
} 

Der Fehler Ich erhalte ist:

Exception in thread "main" java.util.NoSuchElementException at 
    java.util.Scanner.throwFor(Scanner.java:862) at 
    java.util.Scanner.next(Scanner.java:1485) at 
    java.util.Scanner.nextDouble(Scanner.java:2413) at 
    Main.main(Main.java:10) 
+3

Welche Laufzeitfehler? Ich sehe keine. –

+0

Hallo Herr Prokhorov. Sie meinen also, dass dieses Programm erfolgreich ausgeführt und beendet werden würde, oder? Ich verwende derzeit ein Grading-Programm, das meine Schule verwendet, und es heißt, dass es einen Laufzeitfehler gibt ... Genauer gesagt: Ausnahme im Thread "Haupt" java.util.NoSuchElementException \t bei java.util.Scanner .throwFor (Scanner.java:862) \t bei java.util.Scanner.next (Scanner.java:1485) \t bei java.util.Scanner.nextDouble (Scanner.java:2413) \t bei Main.main (Main.java:10) Was bedeutet das? Vielen Dank. – Sean

+3

Ich meine "Sie zeigen nicht, was Ihre Fehler sind." Jetzt tun Sie das, aber Sie sollten es von Anfang an enthalten. –

Antwort

1

Vor dem Aufruf netInt() auf Eingabe Sie müssen überprüfen, ob es eine Eingabe hat. Außerdem müssen Sie Input und Input2 nicht jedes Mal neu initialisieren. In der Tat sollten Sie nur einen Eingang verwenden Scanner

import java.util.Scanner; 
public class Main 
{ 

public static void main(String[] args) 
{ 
    Scanner input = new Scanner(System.in); 
    int radius = 0; 
    if(input.hasNextInt()){ 
     radius = input.nextInt(); 
    } 
    int length = 0; 
    //Scanner input2 = new Scanner(System.in); 
    if(input.hasNextInt()){ 
     length = input.nextInt(); 
    } 
    while ((radius > 1000) || (radius < 1)) 
    { 
     // input = new Scanner(System.in); 
     if(input.hasNextInt()){ 
      radius = input.nextInt(); 
     } 
    } 

    while ((length > 1000) || (length < 1)) 
    { 
     //input2 = new Scanner(System.in); 
     if(input.hasNextInt()){ 
      length = input.nextInt(); 
     } 
    } 

    double area = (radius * radius) * 3.14159; 
    double volume = area * length; 

    System.out.printf("%.1f\n", area); 
    System.out.printf("%.1f\n", volume); 
    } 
} 
+0

hat dies für Sie @Sean funktioniert? –