所以,我试图找出为什么我的程序没有以正确的方式工作。我想在数组中保存东西,但第一个输入总是被忽略。例如,我问用户"你想输入更多的xy?"然后是一个是,我问有多少和最后一个添加的用户类型被忽略,第一个显示。如果它只有一个它甚至不显示。
这是我的代码
import java.util.*;
public class TriangleType {
public static void main(String[] args) {
// TODO Auto-generated method stub
int a , b ,c ,result , total,count;
int x=9;
String triangle = null;
String[] output=new String[110];
//storing types of traingles and their count for the statics display
String[][] triangleTypes={{"gleichseitig","0"} ,{"gleichschenklig","0"},{"spitzwinklig","0"},{"rechtwinklig","0"},{"stumpwinkling","0"}};
//2D array to store the sides of a traingle
int[][] sides=new int[110][3];
sides[0][0]=2; sides[0][1]=2; sides[0][2]=4;
sides[1][0]=3; sides[1][1]=4; sides[1][2]=5;
sides[2][0]=5; sides[2][1]=5; sides[2][2]=5;
sides[3][0]=8; sides[3][1]=6; sides[3][2]=10;
sides[4][0]=10; sides[4][1]=10; sides[4][2]=2;
sides[5][0]=2; sides[5][1]=4; sides[5][2]=9;
sides[6][0]=12; sides[6][1]=36; sides[6][2]=4;
sides[7][0]=5; sides[7][1]=6; sides[7][2]=7;
sides[8][0]=7; sides[8][1]=9; sides[8][2]=12;
//User input
System.out.println("Moechten Sie mehr Dreiecke hinzufuegen? (1/Ja/; 0/Nein/)");
Scanner input = new Scanner(System.in);
String s = input.nextLine();
int equivalent=Integer.parseInt(s);
if(equivalent==1){
System.out.println("Wie viele Dreiecke moechten Sie hinzufuegen?");
Scanner inputt = new Scanner(System.in);
String ss = input.nextLine();
total=Integer.parseInt(ss);
for (int i=0; i<total; i++){
System.out.println("Seite a von Dreieck "+(8+i+1)+" eingeben:");
Scanner input1 = new Scanner(System.in);
String s1 = input1.nextLine();
System.out.println("Seite b von Dreieck "+(8+i+1)+" eingeben:"); Scanner input2 = new Scanner(System.in);
String s2 = input2.nextLine();
System.out.println("Seite c von Dreieck "+(8+i+1)+" eingeben:"); Scanner input3 = new Scanner(System.in);
String s3 = input3.nextLine();
a=Integer.parseInt(s1);
b=Integer.parseInt(s2);
c=Integer.parseInt(s3);
x=9+i;
sides[9+i][0]=a;
sides[9+i][1]=b;
sides[9+i][2]=c;
}}
for(int j=0; j<x; j++){
triangle=evaluateTriangle(sides[j][0], sides[j][1], sides[j][2]);
output[j]=triangle;
System.out.println(sides[j][0]+" "+sides[j][1]+" "+sides[j][2] + " "+triangle);
}
for(int k=0; k<output.length; k++)
{
//check if returned triangle matches with one of defined types in triangleType array
for(int l=0; l<triangleTypes.length; l++ ){
if(output[k]==triangleTypes[l][0])
{ //if yes then update second member indicating no of occurences of that particular traingle
count=(Integer.parseInt(triangleTypes[l][1]));
count=count+1;
triangleTypes[l][1]=Integer.toString(count);
}
}
}
//show statistics
System.out.println("======STATISTIK=======");
System.out.println("Dreieckstyp Anzahl");
for(int m=0; m<triangleTypes.length; m++)
{
System.out.println(triangleTypes[m][0]+" "+triangleTypes[m][1]);
}
}
//function to evaluate the type of triangle
public static String evaluateTriangle(int side1 , int side2 , int side3){
int a,b,c;
a=side1;
b=side2;
c=side3;
if(a <= 0 || b <= 0 || c <= 0)
{return "kein gültiges Dreieck";}
else if(a == b && b == c)
{return "gleichseitiges Dreieck";}
else if((a*a)+(b*b)==(c*c))
{return "rechtwinkliges Dreieck";}
else if((a*a)+(b*b)>(c*c))
{ return "spitzwinkliges Dreieck";}
else if((a*a)+(b*b)<(c*c))
{ return "stumpwinklinges Dreieck";}
else if (a == b || b == c || c == a)
{return "gleichschenkliges Dreieck";}
else return "undefiniert";
}
}
对于长代码很抱歉,但我在这里绝望(也是java的新手)。 提前谢谢!
答案 0 :(得分:1)
System.out.println("Wie viele Dreiecke moechten Sie hinzufuegen?");
Scanner inputt = new Scanner(System.in);
String ss = input.nextLine();
不应该是最后一个input.nextLine()是inputt.nextLine()?
如果要删除理解问题所需的所有行,将更容易调试。
答案 1 :(得分:1)
代码中用于打印结果的for循环不够高,将for循环更改为&lt; = x:
for(int j=0; j<=x; j++){
triangle=evaluateTriangle(sides[j][0], sides[j][1], sides[j][2]);
output[j]=triangle;
System.out.println(sides[j][0]+" "+sides[j][1]+" "+sides[j][2] + " "+triangle);
}
答案 2 :(得分:0)
更改行:
for(int j=0; j<x; j++)
你的for循环没有到达最后一个元素,因为你需要在一个数组中找到11个元素,但你只索引它们中的十个,放:
for(int j=0; j<=x; j++)
以这种方式到达
sides[10][0]
sides[10][1]
sides[10][2]
包含未显示的数据