针对Java初学者的{Sytax问题

时间:2015-11-14 08:40:21

标签: java

我是Java的初学者,我有一个编写销售门票的程序的任务。我有一些问题,我无法理解为什么他们错了。我遇到的问题是编译。

import acm.program.*;
import acm.graphics.*;

public class tickets extends ConsoleProgram {
    public static double eisitirio = 1.2;
    public void run(){
    double nomisma=readInt("Insert coins and then press 0");
    boolean synthiki=false;
    double poso=0;
    while (synthiki=false){
        while (nomisma != 0){
            if ((nomisma==0.1)||(nomisma==0.2)||(nomisma==0.5)||(nomisma==1)||(nomisma==2)||(nomisma==5)){
            poso=poso+nomisma;
            }else {
                printIn("You did not insert a supported coin, please insert another one");
            }
            nomisma=readInt("Insert coins and then press 0");
        }
        if (poso < eisitirio){
            printIn("You did not insert enough money, please insert more coins");
        }else {
            synthiki=true;
        }
    }
    printIn("Here is your ticket");
    poso=poso-eisitirio;
    double resta=0;
    if ((poso/5) > 0){
        printIn("You have change: 5 euros");
        poso = poso-5;
    }
    if ((poso/2) > 0){
        printIn("You have change: 2 euros");
        poso = poso-2;
    }
    if ((poso/1) > 0){
        printIn("You have change: 1 euros");
        poso = poso-1;
    }   
    if ((poso/0.5) > 0){
        printIn("You have change: 50 cents");
        poso = poso-0.5;
    }
    if ((poso/0.2) > 0){
        printIn("You have change: 20 cents");
        poso = poso-0.2;
    }
    if ((poso/0.1) > 0){
        printIn("You have change: 10 cents");
        poso=poso-0.1;
    }
    }

}

编译错误:

C:\Users\kriton\Desktop\Java>javac -cp acm.jar tickets.java
tickets.java:15: error: cannot find symbol
                                printIn("You did not insert a supported coin, please insert another one");
                                ^
  symbol:   method printIn(String)
  location: class tickets
tickets.java:20: error: cannot find symbol
                        printIn("You did not insert enough money, please insert more coins");
                        ^
  symbol:   method printIn(String)
  location: class tickets
tickets.java:25: error: cannot find symbol
        printIn("Here is your ticket");
        ^
  symbol:   method printIn(String)
  location: class tickets
tickets.java:29: error: cannot find symbol
                printIn("You have change: 5 euros");
                ^
  symbol:   method printIn(String)
  location: class tickets
tickets.java:33: error: cannot find symbol
                printIn("You have change: 2 euros");
                ^
  symbol:   method printIn(String)
  location: class tickets
tickets.java:37: error: cannot find symbol
                printIn("You have change: 1 euros");
                ^
  symbol:   method printIn(String)
  location: class tickets
tickets.java:41: error: cannot find symbol
                printIn("You have change: 50 cents");
                ^
  symbol:   method printIn(String)
  location: class tickets
tickets.java:45: error: cannot find symbol
                printIn("You have change: 20 cents");
                ^
  symbol:   method printIn(String)
  location: class tickets
tickets.java:49: error: cannot find symbol
                printIn("You have change: 10 cents");
                ^
  symbol:   method printIn(String)
  location: class tickets
9 errors

我不知道,我已经尝试了很多东西,但它们看起来很奇怪。我不明白为什么第一个是错的,我在书中查了一下,关于ifs和booleans,但看起来是正确的。

3 个答案:

答案 0 :(得分:3)

  • 对于初学者来说,那里的System.out.printlnprintIn

  • readInt应该更改为:

    BufferedReader br = new BufferedReader( new InputStreamReader( System.in ) );
    
    System.out.print( "Insert coins and then press 0" );
    nomisma = Integer.parseInt( br.readLine() );
    // This is the PURE implementation of the function you want; now, if you haven't defined this somewhere, it will cause you problems.
    

答案 1 :(得分:0)

包含库并将 printIn 替换为 System.out.println

答案 2 :(得分:0)

  

错误:找不到符号

意味着......

......无法找到符号。

原因是......

......没有定义符号。

可能的解决方案:

  • 定义符号
  • 使用定义的符号

在您的情况下,您尝试调用名为printIn的方法。在我们的例子中,符号为printIn。由于未定义,因此您收到上述错误。您可以像这样定义符号:

public static void printIn(text) {
    System.out.println(text);
}

或使用System.out.println代替。阅读更多here