这个问题在这里已有答案:
Java:文字百分号签到printf语句 3答案我最近一直在尝试java,但我现在遇到了这个问题:
我不知道如何在java中打印'%'符号。
如果有人可以提供帮助,请做!
这是我的代码:
import java.util.Scanner; class Averager { public static void main(String args[]) { //A Simple Averaging Program Scanner input = new Scanner(System.in); //Declare variables: double total = 0; double counter = 0; long mark; double average; int numOfPupils; int totalMarks; //Ask how many papers the teacher would like to average: System.out.println("How many student's papers would you like to avarage?"); numOfPupils = input.nextInt(); //Ask how many marks were available to the student to earn: System.out.println("How many marks were available to the student?"); totalMarks = input.nextInt(); //Repeat the amount of papers times: while (counter < numOfPupils) { //Ask how many marks the student got: System.out.printf("How many marks did student number %s get? \nStudent %s scored: ", counter + 1, counter + 1); mark = input.nextLong(); total += mark; System.out.printf("Student %s scored: %s %", counter + 1, ((100 / totalMarks) * mark)); counter++; } average = total / (counter); System.out.printf("The average was %s marks.", average); input.close(); } }...它说我的帖子主要是代码,所以我会在这里添加更多细节。
我正在使用eclips Mars 1
我大约3天前开始使用java - 一位朋友教我如何使用它。
提前谢谢了,
Sheikh1365
This question already has an answer here:
Java: Literal percent sign in printf statement 3 answersHow do I print a % sign in Java? I have tried "\%", which doesn't seem to work. Any ideas?
最满意答案
贝娄是纠正的代码,如上所述你应该使用双%来逃避%,即: %%
我还添加了一些新的行字符,使应用程序看起来更整洁。
import java.util.Scanner; public class test { public static void main(String args[]) { //A Simple Averaging Program Scanner input = new Scanner(System.in); //Declare variables: double total = 0; double counter = 0; long mark; double average; int numOfPupils; int totalMarks; //Ask how many papers the teacher would like to average: System.out.println("How many student's papers would you like to avarage?"); numOfPupils = input.nextInt(); //Ask how many marks were available to the student to earn: System.out.println("How many marks were available to the student?"); totalMarks = input.nextInt(); //Repeat the amount of papers times: while (counter < numOfPupils) { //Ask how many marks the student got: System.out.printf("\nHow many marks did student number %s get? \nStudent %s scored: ", counter + 1, counter + 1); mark = input.nextLong(); total += mark; System.out.printf("Student %s scored: %s%%", counter + 1, ((100 / totalMarks) * mark)); counter++; } average = total / (counter); System.out.printf("\nThe average was %s marks.", average); input.close(); } }偏离主题但可能需要考虑的是,在某些情况下,您的代码会失败,因为您需要处理一些无效输入。
Bellow is the code corrected, as mentioned above you should use double % to escape %, i.e: %%
I have also added some new line characters to make the app look more neat.
import java.util.Scanner; public class test { public static void main(String args[]) { //A Simple Averaging Program Scanner input = new Scanner(System.in); //Declare variables: double total = 0; double counter = 0; long mark; double average; int numOfPupils; int totalMarks; //Ask how many papers the teacher would like to average: System.out.println("How many student's papers would you like to avarage?"); numOfPupils = input.nextInt(); //Ask how many marks were available to the student to earn: System.out.println("How many marks were available to the student?"); totalMarks = input.nextInt(); //Repeat the amount of papers times: while (counter < numOfPupils) { //Ask how many marks the student got: System.out.printf("\nHow many marks did student number %s get? \nStudent %s scored: ", counter + 1, counter + 1); mark = input.nextLong(); total += mark; System.out.printf("Student %s scored: %s%%", counter + 1, ((100 / totalMarks) * mark)); counter++; } average = total / (counter); System.out.printf("\nThe average was %s marks.", average); input.close(); } }Off-topic but may need consideration, is that your code will fail in some cases, as you need to handle some invalid inputs.
更多推荐
发布评论