【问题标题】:Java: How to continue even with a stringJava:即使使用字符串也如何继续
【发布时间】:2021-07-19 05:39:19
【问题描述】:

所以我在我的代码中迷失了一些东西。首先,当我在控制台上输入字符串时,在询问三角形的高度后,它不会继续循环。其次,问了高度之后,就不会继续下一个问题了。

代码:

 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(); 

  }
}

【问题讨论】:

  • 调用System.exit(0) 会终止你的程序。从您的程序中删除这些调用。

标签: java project


【解决方案1】:

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();

这会起作用。

【讨论】:

  • 它有效。但是,当我使用半径语句时,它不会继续。
  • 您是否删除了所有 Scanner.hasNextInt() 语句?如果是,请分享更新后的代码 sn-p。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-11-02
  • 2020-08-23
  • 2013-12-18
  • 2017-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多