2011-03-16 95 views
0

我试图获取用户输入的最高和最低编号。我懂了。我只是新编程。有3个错误。Java代码中的错误

import java.io.*; 
public class HighestToLowest 
{ 
    public static void main(String []args)throws IOException{ 
    { 
     BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));   

     double[] input = new double[8]; 
     int index; 
     int highIndex = 0; 
     int lowIndex = 0; 
     double sum = 0; 

     System.out.println("Enter The Scores Of Judges: "); 
     for (index = 0; index<8; index++){ 
      System.out.print("Enter The Score" + (index + 1) + ": "); 
      input[index] = Double.parseDouble(dataIn.readLine()); 
     } 

     for (index = 1; index < 8; index++) 
      if(input[highIndex] < input[index]) 
       highIndex = index; 


     for (index = 1; index < 8; index++) 
      if (input[lowIndex] > input[index]) 
       lowIndex = index; 

     for (index = 0; index < 8; index++) 
      sum = sum + input[index]; 
     try 
      { 
       input[index] = Double.parseDouble(dataIn.readLine()); 
      } 
      catch(IOException e) 
      { 
       System.out.println("error"); 
      } 

      if(sum>index) 
      { 
       sum=highIndex; 
      } 

      if(sum>=index) 
      { 
       index=lowIndex; 
      }       
     } 

     System.out.print("Highest is: + highIndex"); 
     System.out.print("Lowest is: + lowIndex"); 

     System.out.printf("The Contestant Receives a total of %.2f", (sum - highIndex - lowIndex)); 



    } 
} 
+6

有什么错误? – nos 2011-03-16 16:50:22

+4

无论何时您遇到错误消息,请始终在提示您提供问题的线路和完整的消息中发帖。 – jzd 2011-03-16 16:50:48

+3

下次尝试将问题放在问题的“正文”中,而不是“标题”。 – adarshr 2011-03-16 16:50:54

回答

0

虽然你说只有3个错误,有认为是多一点

public static void main(String []args)throws IOException{ //<- ? 
{//<- why two curly brackets? 

在for循环

for (index = 0; index < 8; index++){//<- curl bracket missing? 
     sum = sum + input[index]; 
     try { 
       input[index] = Double.parseDouble(dataIn.readLine()); 
      } catch(IOException e) { 
       e.printStackTrace(); 
     } 

     if(sum>index) { 
       sum=highIndex; 
      } 
      if(sum>=index){ 
       index=lowIndex; 
      }       
    } // <- or extra curl bracket? 

这条线将打印Highest is: + highIndex

System.out.print("Highest is: + highIndex"); 

0中的任何东西"按原样打印。

所以将其更改为

System.out.print("Highest is:" + highIndex); 

同样适用于

System.out.print("Lowest is: + lowIndex"); 

你是从C语言编程,这条线是正确的

System.out.printf("The Contestant Receives a total of %.2f", (sum - highIndex - lowIndex)); 

在Java中也可以写为

System.out.println("The Contestant Receives a total of " + (sum - highIndex - lowIndex));