2013-03-08 54 views
-1

我有以下的分配问题:决策组织结构的Java

创建一个程序,提示用户输入两个值:电影分级和他或她的年龄。使用决策结构,根据输入的评分和年龄确定是否允许用户在影院中观看电影。最后,将这个决定的结果显示给用户。

这里是我的代码,我做了键Alt + Shift + F在NetBeans我张贴在此之前:

/* 
    * To change this template, choose Tools | Templates 
    * and open the template in the editor. 
    */ 
    package age; 

    /** 
    * 
    * @author Jason 
    */ 
    import java.util.Scanner; 

    public class Age { 

     public static void main(String[] args) { 

      Scanner user_input = new Scanner(System.in); 


      String user_age; 
      System.out.print("Enter your age: "); 
      user_age = user_input.next(); 

      String mrating; 
      System.out.print("Enter the movie rating: "); 
      mrating = user_input.next(); 

     } 

    { 
     if (user_age < 13 + mrating = G);{ 
     System.out.print("You are of the right age for this movie"); 
     } 
     else{ 
     System.out.print(" You are not of correct age for this movie"); 
     } 

     if (user_age >= 13 + mrating = PG13);{ 
     System.out.print("You are of the right age for this movie"); 
     } 
     else{ 
     System.out.print(" You are not of correct age for this movie"); 
     } 

     if (user_age >= 17 + mrating = R);{ 
     System.out.print("You are of the right age for this movie"); 
     } 
     else{ 
     System.out.print(" You are not of correct age for this movie"); 
     } 

    } 
} 

如果我提出我的尾架的年龄到if语句开始之前。我可以让显示器询问用户的年龄和评分,然后程序结束,没有结果。如果我将支架留在那里,程序完全失败。我是Java新手,感到非常困惑,我一直在为这本书和网站上的小时工作,但我开始感到困惑。此外,user_agemrating出现错误,说不使用变量。

+0

您的类中有一段代码没有引用尚未声明的变量。在这个模块中if语句的多个部分有悬挂分号 - 例如('if(user_age <13 + mrating = G); {')。这是你的真实代码吗? – Perception 2013-03-08 00:52:22

+0

请注意,第一个右大括号是缩进以与您的'main()'方法对齐。这意味着'if'语句不在该方法中。删除该大括号和下面的大括号,然后再次执行Alt-Ctrl-F。 – 2013-03-08 00:53:44

+0

@Perception“* not *引用具有* not *声明的变量”没有问题。 – 2013-03-08 01:00:16

回答

-1

由于这是任务,我也不会完全的帮助....你必须确保现在你的程序的逻辑正确性...

我已经解决了所有编译错误......很多语法问题..请通过一些初学java的书。

下面

将让你开始...

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package age; 

/** 
* 
* @author Jason 
*/ 
import java.util.Scanner; 

public class Age 
{ 

    public static void main(String[] args) 
    { 

     Scanner user_input = new Scanner(System.in); 

     System.out.print("Enter your age: "); 
     int user_age = Integer.parseInt(user_input.next()); 

     String mrating; 
     System.out.print("Enter the movie rating: "); 
     mrating = user_input.next(); 

     if (user_age < 13 && mrating == "G") 
     { 
      System.out.print("You are of the right age for this movie"); 
     } 
     else 
     { 
      System.out.print(" You are not of correct age for this movie"); 
     } 

     if (user_age >= 13 && mrating == "PG13") 
     { 
      System.out.print("You are of the right age for this movie"); 
     } 
     else 
     { 
      System.out.print(" You are not of correct age for this movie"); 
     } 

     if (user_age >= 17 && mrating == "R") 
     { 
      System.out.print("You are of the right age for this movie"); 
     } 
     else 
     { 
      System.out.print(" You are not of correct age for this movie"); 
     } 

    } 
} 
+1

解释OP可以做的事情比仅仅发布代码更有价值解释,特别是当他不明白发生了什么事情时。 – adchilds 2013-03-08 00:54:35

+0

我同意你@adchilds ...只是说,有太多的语法错误..在语言中指出很多努力....可能他会理解一些,而不是一些...所以最好的方式,我虽然会提供语法正确的代码,以便他可以比较并知道错在什么地方...... – Amit 2013-03-08 00:56:44

1

首先,你不能比较一个String(例如user_age)为整数。在与另一个整数比较之前,使用Integer.parseInt(String)转换为int

其次,您不应该使用==来比较两个字符串值。使用equals方法来比较两个字符串值。

第三,使用&&布尔运算符来表示“and”而不是+

第四,删除紧跟在每条if语句的条件之后的分号。否则,如果条件为真,编译器会认为;是要执行的块。

可能存在其他错误;这是我发现的第4个。

0

您需要确保您的所有代码都位于main()方法内。这是使用Alt-Ctrl-F格式化代码的原因之一。如果您在代码中检查大括号,则会看到main()if语句之前结束。您可以通过在您的if声明之前删除关闭和开启括号来解决此问题。

您的代码中可能还存在其他问题。如果您需要修复它们的帮助,请发布完整的编译器错误,以便我们可以为您提供帮助。