Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java程序员在输出时感到困惑(构造函数)_Java_Input_Compiler Construction_Io_Case - Fatal编程技术网

Java程序员在输出时感到困惑(构造函数)

Java程序员在输出时感到困惑(构造函数),java,input,compiler-construction,io,case,Java,Input,Compiler Construction,Io,Case,我是一个相当新的Java程序员,目前正在学习一个在线教程来提高我的技能。我在教程中找到了下面的代码示例,这些代码看起来都应该运行,但是当我在Eclipse中运行代码时,我遇到了一些错误 这是我第一个使用cases的程序,但是我很确定我有正确的语法,也许有人能指出我的错误?我还感到惊讶的是,编译器抱怨行“System.out.println”(“您确定(y-yes,n-no)”) 第3、4、7、8、10、11、15行出现错误。有人能告诉我为什么我的程序不能运行吗 class Certainty

我是一个相当新的Java程序员,目前正在学习一个在线教程来提高我的技能。我在教程中找到了下面的代码示例,这些代码看起来都应该运行,但是当我在Eclipse中运行代码时,我遇到了一些错误

这是我第一个使用cases的程序,但是我很确定我有正确的语法,也许有人能指出我的错误?我还感到惊讶的是,编译器抱怨行“System.out.println”(“您确定(y-yes,n-no)”)

第3、4、7、8、10、11、15行出现错误。有人能告诉我为什么我的程序不能运行吗

class Certainty  
{
    System.out.println ("Are you sure (y - yes, n - no)?");
    int ch = System.in.read ();
    switch (ch)
    {
       case 'Y':
       case 'y': System.out.println ("I am sure.");
                break;
       case 'N':
       case 'n': System.out.println ("I am not sure.");
                 break;
       Default : System.out.println ("Incorrect choice.");
    }
}

/*谢谢你们所有的有用的回答,我正慢慢开始了解Java,我真的很喜欢它,也很喜欢我的问题能很快得到回答,你们真是太棒了。**/

你们应该把这些行放在
main
方法或任何其他方法中。
例如:

这是我第一个使用cases的程序,但是我很确定我有正确的语法,也许有人能指出我的错误

大多数关于case的实际语法都是正确的,除了它是
default:
而不是
default:
(大写很重要)

但是您的类在类内部有一步一步的代码。你不能那样做。它必须位于初始值设定项或(更常见的)构造函数和/或方法内部

另外,
System.in.read()
可能抛出一个
IOException
,您必须声明您的方法抛出或捕获该异常。在下面的例子中,我抓住了它,只是说它发生了。通常情况下,你会做一些比这更有用的事情,但是对于这种快速测试来说,这很好

import java.io.IOException;  // <=== Tell the compiler we're going to use this class below
class Certainty  
{
    public static final void main(String[] args) {
        try {                      // `try` starts a block of code we'll handle (some) exceptions for
            System.out.println ("Are you sure (y - yes, n - no)?");
            int ch = System.in.read ();
            switch (ch)
            {
               case 'Y':
               case 'y': System.out.println ("I am sure.");
                        break;
               case 'N':
               case 'n': System.out.println ("I am not sure.");
                         break;
               default : System.out.println ("Incorrect choice.");
            }
        }
        catch (IOException e) {    // <== `catch` says what exceptions we'll handle
            System.out.println("An exception occurred.");
        }
    }
}

import java.io.IOException;//你的main方法在哪里?你的类中没有任何方法。同样,我很确定你需要先将ch转换成char,然后才能使用switch:switch((char)ch)你可以在类体中声明变量。但是这些语句应该是某些方法体的一部分,以便更好地工作you@rhobincu不。这不是必需的。紧接着
类{…}
中出现了字段和方法的声明。@JoopEggen如果我们在类中除了main方法之外没有任何实例变量和方法怎么办?我表达得不清楚;我想在你的回答中加上解释;类主体中的立即数不是代码,而是字段和方法(以及构造函数、初始值设定项、内部类)的声明。+1尽管它需要一个
抛出IOException
或类似的编译。@Duncan:是的,添加了。感谢您更正我的代码并解释它@先生,你是位绅士和学者。
import java.io.IOException;  // <=== Tell the compiler we're going to use this class below
class Certainty  
{
    public static final void main(String[] args) {
        try {                      // `try` starts a block of code we'll handle (some) exceptions for
            System.out.println ("Are you sure (y - yes, n - no)?");
            int ch = System.in.read ();
            switch (ch)
            {
               case 'Y':
               case 'y': System.out.println ("I am sure.");
                        break;
               case 'N':
               case 'n': System.out.println ("I am not sure.");
                         break;
               default : System.out.println ("Incorrect choice.");
            }
        }
        catch (IOException e) {    // <== `catch` says what exceptions we'll handle
            System.out.println("An exception occurred.");
        }
    }
}