如何在Java中打印%符号?我尝试了“ \%”,它似乎不起作用。有任何想法吗?
波纹管的代码已更正,如上所述,您应使用double%来转义%,即: %%
我还添加了一些换行符,以使应用看起来更简洁。
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();
}
}
没必要,但可能需要考虑的是,在某些情况下您的代码将失败,因为您需要处理一些无效的输入。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句