有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java我输入的数字没有按升序排序,三角形似乎总是有效的,无论数字是什么。有什么建议吗?

我输入的数字没有按升序排序,三角形似乎总是有效的,无论数字是什么。有什么建议吗

这是我的密码:

import java.util.Scanner;

/** An exercise of using methods */

public class MethodExercise {

/**main method */
//Group A fill in the blanks for this method
public static void main (String[] args) {
  double num1, num2, num3, avg, max;


  //Get input for the three numbers
  Scanner kb = new Scanner(System.in);

  System.out.print("Enter the first number: ");      
  num1 = kb.nextDouble();

  System.out.print("Enter the second number: ");
  num2 = kb.nextDouble();

  System.out.print("Enter the third number: ");
  num3 = kb.nextDouble();



  //Call calcAverage method 
  avg=(num1+num2+num3)/3;

 //Output the result
  System.out.println("The average of the three numbers is " + avg);

  //Call findMax method
  double[] myList = {num1,num2,num3};
  myList = new double[3];
  myList[0] = num1;
  myList[1] = num2;
  myList[2] = num3;
  max = myList[0];
  for (int i = 1;i<myList.length;i++){
  if(myList[i]>max)
    max = myList[i];
  }

  //Output max
  System.out.println("The maximum number of the three numbers is " + max);

  //Call sort method
 System.out.println("The sorted int array is:");
 for (double number : myList) {
System.out.println("Number = " + number);

   }

  //Call isValidTriangle method
   boolean isTriangle = true||false; 

    if ((num1+num2>num3)||(num1+num3>num2)||(num2+num3>num1)){
        isTriangle = true;
     }

  if (isTriangle == true){
    System.out.println("It is a valid triangle");
    }
  else 
    System.out.println("It is not a valid triangle");

  } //end main

  /**Method calcAverage: calculates and returns the average of the three numbers*/

 /**Method findMax: finds and returns the maximum number among the three numbers*/

 /**Method sort: sorts and displays the three numbers in increasing order*/

 /**Method isValidTriangle: determines if the three numbers (representing three edges)
      can form a valid triangle*/

  } //end class

共 (2) 个答案

  1. # 1 楼答案

    看起来好像你应该把代码放到他们自己的方法中,但我将把这留给你。。。至于你的分类方法,需要做一些工作。这是我所拥有的

    double[] myList = {num1,num2,num3};
    double temp;
    for (int i = 1; i < myList.length; i++) {
       for(int j = i ; j > 0 ; j ){
          if(myList[j] < myList[j-1]){
             temp = myList[j];
             myList[j] = myList[j-1];
             myList[j-1] = temp;
          }
       }
    }
    
    //Output max
    System.out.println("The maximum number of the three numbers is " + myList[2]);
    

    而且,这是多余的

    double[] myList = {num1,num2,num3};
    myList = new double[3];
    myList[0] = num1;
    myList[1] = num2;
    myList[2] = num3;
    

    在第一行中,指定数组中的值,然后用myList = new double[3];清空数组,然后再次填充数组

    至于测试三角形是否可能,您的“isTriangle”变量开始为true(因为true | | false为true),并且无法变为false。而且,你的测试根本上是有缺陷的。想想看。边为1、2和100的三角形绝对不是三角形;你的方法说这是因为

    1 + 2 > 100 is false
    1 + 100 > 2 true
    2 + 100 > 1 true
    
    false || true || true is true
    

    应该是这样的。执行每一个计算,如果其中任何一个都是错误的,那么它就不可能是一个三角形

    boolean isTriangle = true;
    if (!(num1+num2>num3)||!(num1+num3>num2)||!(num2+num3>num1)){
       isTriangle = false;
    }
    
    if (isTriangle == true){
       System.out.println("It is a valid triangle");
    } else {
       System.out.println("It is not a valid triangle");
    }
    

    嗯,我想就是这样。尽管您应该将这些代码组中的每一组放在它们自己的方法中

  2. # 2 楼答案

    您从未将isTriangle设置为false。下面的语句将其设置为true,然后您将再次从执行比较的位置将其设置为true

    /* This will set isTriangle to true because (TRUE OR FALSE) always evaluates to TRUE */
    boolean isTriangle = true||false;
    

    您应该这样做:

    boolean isTriangle = false;