Java:如何继续使用字符串

Java: How to continue even with a string

所以我对我的代码中的一些事情感到困惑。首先,当我在控制台上输入字符串时,在询问三角形的高度后,它不会继续循环。其次,问完身高就不会再继续下一题了。

代码:

 import java.util.Scanner;

 class Assignment01 {
 public static void main(String[] args) {

// Let's add the scanner object 
    Scanner cin = new Scanner(System.in); 
    
    // Asking the user's name
    System.out.println("Please enter your name"); 
    
    String yourName = cin.nextLine(); 
    
    // Triangle 
    // Variables 
    int height = 0; 
    int base = 0;  
    
    // Enter the height of the triangle 
    System.out.println("Hello, " + yourName + ". Please enter the height of a right triangle: ");  
    
    // Let's create a loop
    
    height = cin.nextInt();
    
    
        if (cin.hasNextInt()) {
            cin.next(); 
            
    if (height < 0 || height > 1000) {
            cin.next();
    System.exit(0);
          } 

        } else {
            System.out.println("Invalid value");
            System.exit(0);
        }
    
    
    // Enter the base of the triangle
     System.out.println("Now enter the base of the same right triangle");
    
    base = cin.nextInt(); 
    
    
        if (cin.hasNextInt()) {
            cin.next(); 

    if (base < 0 || base > 1000) {
            cin.next();
    System.exit(0);
          } 
        }  else {
            System.out.println("Invalid value");
            System.exit(0);
        }
    
    
    // The results of the triangle's area 
    System.out.println("The area of this triangle is :"  + ((height * base)/2) ); 
    
    // Circle 
    // Variables 
    double radius = 0; 
    double PI = 0;   
    
    // Enter the radius of the circle 
    System.out.println("Now enter the radius of the circle: "); 
    
    radius = cin.nextInt();
    
        if (cin.hasNextInt()) {
            cin.next(); 

    if (radius <= 0 || radius >= 1000) {
            cin.next();
    System.exit(0);
          }
        } else {
            System.out.println("Invalid value");
            System.exit(0);
        }
    
    
    // Let's print out the area of the circle 
    System.out.println("The area of the circle is " + Math.PI * (Math.pow(radius, 2)) );
    
    // Let's print out the perimeter of the circle 
    System.out.println("The perimeter of the circle is " + 2 * Math.PI * radius ); 
    
    // Let's ask the million dollar question! 
    System.out.println("Can the triangle cover the circle?"); 
    
    String answer = cin.nextLine(); 
    
    double triangleArea = ((height * base)/2); 
    double circleArea = Math.PI*(Math.pow(radius, 2));
    
    if (answer.equals("No") && triangleArea >= circleArea || answer.equals("Yes") && triangleArea <= circleArea) {
        cin.next(); 
        System.out.println("That is wrong."); 
    } 
    else {
        System.out.println("Enter yes or no."); 
    } 
    
    // Time to close the scanner 
    cin.close(); 

  }
}

Scanner.hasNextInt() 将等待下一次输入。删除该代码。

    height = cin.nextInt();

    if (height < 0 || height > 1000) {
        System.exit(0);
    }

    // Enter the base of the triangle
    System.out.println("Now enter the base of the same right triangle");

    base = cin.nextInt();

这会起作用。