我已经开始学习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 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?
答案 0 :(得分:5)
您无法在Java中的方法内定义方法,请将此代码移出main()
方法:
static void print(String s){
System.out.println(s);
}
仔细看看你的大括号,这是明确的代码缩进有用的地方。
答案 1 :(得分:2)
计算您的开始和结束花括号 - 您已经关闭。您缺少一个右大括号,因此,看起来您在另一个方法中声明了一个方法。
答案 2 :(得分:2)
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);
}
}
答案 3 :(得分:0)
可能你的意思是在第27行之前还有一个结束大括号,这样static void print(String s)
就不会里面 {{1} }
答案 4 :(得分:0)
Static不会也不应该进入main。你可以像调用print("String printed");
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);
}
}