如何在java中打印'%'符号[重复](How to print a '%' sign in java [duplicate])

编程入门 行业动态 更新时间:2024-10-23 10:32:10
如何在java中打印'%'符号[重复](How to print a '%' sign in java [duplicate])

这个问题在这里已有答案:

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 answers

How 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.

更多推荐

本文发布于:2023-07-06 07:31:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1047342.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:符号   如何在   java   duplicate   print

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!