【问题标题】:Error while copiling Java file (Arrays,Nested if statements)编译 Java 文件时出错(数组、嵌套 if 语句)
【发布时间】:2015-07-10 18:00:37
【问题描述】:

我一直在尝试编写一段练习代码来计算如果这本书丢失,图书馆将发出的罚款取决于日期月份等的差异...... 这是编译器消息:

Solution.java:26: error: illegal start of expression
         if((givDate[1]-expDate[1])>0){
         ^
Solution.java:25: error: not a statement
   if((givDate[2]-expDate[2]) == 0) (
                                    ^
Solution.java:26: error: ';' expected
         if((givDate[1]-expDate[1])>0){
                                      ^
Solution.java:31: error: 'else' without 'if'
         else{
         ^
Solution.java:37: error: illegal start of expression
    )
    ^
Solution.java:38: error: 'else' without 'if'
     else{
    ^
6 errors

代码如下:

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

public static void main(String[] args) {
 Scanner input = new Scanner(System.in);
 int[] expDate = new int[3] ;
 int[] givDate = new int[3] ;
 for(int i=0;i<3;i++){
    givDate[i]=input.nextInt(); 
 }
 for(int j=0;j<3;j++){
    expDate[j]=input.nextInt(); 
 }
 int fine =  testForStuff(givDate,expDate);
System.out.println(fine);

}
public static int testForStuff(int givDate[],int expDate[]){
   int fine=0;
   if((givDate[2]-expDate[2]) == 0) (
         if((givDate[1]-expDate[1])>0){

         fine = 500*(givDate[1]-expDate[1]);

         }
         else{
             fine = 15*(givDate[0]-expDate[0]);

         }


    )
    else{

    fine = 10000;

    }   

 return fine;


 }    


 }

【问题讨论】:

    标签: java arrays if-statement compiler-errors


    【解决方案1】:

    您在应该使用{} 花括号的地方使用() 括号。

    if((givDate[2]-expDate[2]) == 0) (  // invalid: parenthesis
         if((givDate[1]-expDate[1])>0){
    
         fine = 500*(givDate[1]-expDate[1]);
    
         }
         else{
             fine = 15*(givDate[0]-expDate[0]);
    
         }
    
    
    ) // invalid: parenthesis
    

    应该是

    if((givDate[2]-expDate[2]) == 0) { // curly braces
         if((givDate[1]-expDate[1])>0){
    
         fine = 500*(givDate[1]-expDate[1]);
    
         }
         else{
             fine = 15*(givDate[0]-expDate[0]);
    
         }
    
    
    } // curly braces
    

    【讨论】:

    • 在第 28 行和第 37 行查看它们,如编译器错误所示。
    【解决方案2】:
    Solution.java:25: error: not a statement
    
    if((givDate[2]-expDate[2]) == 0) **(**
    

    有一个弯曲的双引号(用**制作)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-17
      • 1970-01-01
      相关资源
      最近更新 更多