当前位置: 首页 > 知识库问答 >
问题:

我用java写了一个代码,由于某种原因我得到了错误[重复]

郁烨
2023-03-14
/*The code should pick up your gander (m\f)
you have to fill in your results.
If you are female you fill in seven results and if you are male you fill ten.
At the end, the code writes you the amount of all your results together
  */
import java.util.*;
public class gameScore {
  public static void main (String[] args) {
    Scanner scanner = new Scanner(System.in);
    int i=0, place=0, finalScore=0;
    
    //askes you for your gender until you inserting valid answer
    while (i==0) {
      System.out.println("Please insert your gender (M\F): ");
      char gender = scanner.next().charAt(0);
      if (gender=='f' || gender=='F' || gender=='m' || gender=='M') {
        i++;
      }
    }
   
    //if you inserted m\M:
    if (gender == 'm' || gender == 'M') {
      for (i=0; i<10; i++) {
        System.out.println("Please insert your score in game number " + (i+1) + ": ");
        int score = scanner.nextInt();
        finalScore += score;
      }
    }

//if you inserted f\F:
if (gender == 'f' || gender == 'F') {
  for (i=0; i<7; i++) {
    System.out.println("Please insert your score in game number " + (i+1) + ": ");
    int score = scanner.nextInt();
    finalScore += score;
  }
}

//prints the final score
System.out.println("your final score is: " + finalScore);

} }

/*我得到的错误是:文件:C:\Users\avino\Documents\java_project\gameScore。java[line:14]错误:转义序列无效(有效的是\b\t\n\f\r“'\)*/

共有2个答案

水睿
2023-03-14

你的问题就在这里

System.out.println("Please insert your gender (M\F): ");

转义序列是(\n,\b,\t等),您使用(M\F)作为您的要求,java认为\F是一个转义序列。所以,让你的(M\F)像下面这样

System.out.println("Please insert your gender (M\\F): ");
长孙智刚
2023-03-14

在这行中使用\而不是\

  System.out.println("Please insert your gender (M\F): ");

所以:

  System.out.println("Please insert your gender (M\\F): ");
 类似资料: