为什么这个java初学者程序会出错
我已经开始学习java,并编写了一些练习。请告诉我哪里出错了,它说我在第27行有一个非法的开始表达为什么这个java初学者程序会出错,java,Java,我已经开始学习java,并编写了一些练习。请告诉我哪里出错了,它说我在第27行有一个非法的开始表达 import java.util.Scanner; public class steps { public static void main(String []args) { Scanner Keyboard = new Scanner(System.in); print("What is your name?"); String
import java.util.Scanner;
public class steps
{
public static void main(String []args)
{
Scanner Keyboard = new Scanner(System.in);
print("What is your name?");
String name = Keyboard.nextLine();
print("What is five + five?");
String number = Keyboard.nextLine();
String gj = (", Good Job");
switch (number){
case "ten":
print("correct" + gj + (" ") + name);
break;
case "Ten":
print("correct" + gj +(" ") + name);
break;
case "10":
print("correct" + gj +(" ") + name);
break;
default:
print("Wroung try again");
}
static void print(String s) { // <--- this is line 27
System.out.println(s);
}
}
// <--- there is no trailing } here?
import java.util.Scanner;
公共类步骤
{
公共静态void main(字符串[]args)
{
扫描仪键盘=新扫描仪(System.in);
打印(“你叫什么名字?”);
字符串名称=Keyboard.nextLine();
打印(“五加五是什么?”);
字符串编号=Keyboard.nextLine();
字符串gj=(“,干得好”);
开关(编号){
案例“十”:
打印(“正确”+gj+(“”)+姓名);
打破
案例“十”:
打印(“正确”+gj+(“”)+姓名);
打破
案例“10”:
打印(“正确”+gj+(“”)+姓名);
打破
违约:
打印(“重新尝试”);
}
静态void print(String s){//计算开始和结束时的花括号--您已禁用。您缺少一个结束大括号,因此,看起来您在另一个方法中声明了一个方法。如果无法在Java中的方法中定义方法,请将此代码移出main()
方法:
static void print(String s){
System.out.println(s);
}
还要仔细看大括号,这是代码清晰缩进的地方。嵌套方法在java中是不允许的。嵌套方法表示另一个方法中的方法。在您的情况下,print
方法包含在main
方法中,这是不允许的
你喜欢这样吗
import java.util.Scanner;
public class steps
{
public static void main(String []args)
{
Scanner Keyboard = new Scanner(System.in);
print("What is your name?");
String name = Keyboard.nextLine();
print("What is five + five?");
String number = Keyboard.nextLine();
String gj = (", Good Job");
switch (number){
case "ten":
print("correct" + gj + (" ") + name);
break;
case "Ten":
print("correct" + gj +(" ") + name);
break;
case "10":
print("correct" + gj +(" ") + name);
break;
default:
print("Wroung try again");
}
}
static void print(String s){
System.out.println(s);
}
}
可能您想要的是在第27行之前再加一个右括号,这样静态void print(String s)
就不会在公共静态void main(String[]args)
static不会也不应该进入main。您可以像这样调用main内部的print方法print(“字符串打印”);
您的函数print不需要是静态的。只需`void print(String s){}即可。ps:如果找到答案,请确保单击它旁边的复选标记。
import java.util.Scanner;
public class steps {
public static void main(String[] args) {
Scanner Keyboard = new Scanner(System.in);
System.out.print("What is your name? ");
String name = Keyboard.nextLine();
System.out.print("What is five + five? ");
String number = Keyboard.nextLine();
String gj = (", Good Job");
switch (number) {
case "ten":
System.out.print("correct" + gj + (" ") + name);
break;
case "Ten":
System.out.print("correct" + gj + (" ") + name);
break;
case "10":
System.out.print("correct" + gj + (" ") + name);
break;
default:
while (Integer.parseInt(number)!= 10){//Checks if number isn't correct
//and ask user to input again until he gets it right.
System.out.print("Wrong try again :");
number = Keyboard.nextLine();
if(Integer.parseInt(number)==10){
System.out.print("correct" + gj + (" ") + name)
}
}
}
print("String printed");
}
static void print(String s) {
System.out.println("\n"+s);
}
}