Discuss / Java / 作业

作业

Topic source

        //练习

        //请帮小明同学设计一个程序,

        //输入上次考试成绩(int)和本次考试成绩(int),

        //然后输出成绩提高的百分比,保留两位小数位(例如,21.75%)

        int last, score;

        double res = .0;

        System.out.print("plase input your last score: ");

        last = scanner.nextInt();

        System.out.print("plase input your score: ");

        score = scanner.nextInt();

        if (last < score){

            res = (double)score / (double)last;

            System.out.print("上升了:");

        }

        else if (last > score){

            res = (double)last / (double)score;

            System.out.print("下降了:");

        }

        else{

            System.out.print("没有变化:");

        }

        res = res * 100.0;

        System.out.printf("%.2f%%", res);

#2 Created at ... [Delete] [Delete and Lock User]

public class liushu {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in); // 创建Scanner对象

        System.out.print("Input your last score: "); // 打印提示

        int fscore = scanner.nextInt(); // 读取一行输入并获取字符串

        System.out.print("Input your this score: "); // 打印提示

        int tscore = scanner.nextInt(); // 读取2行输入并获取整数

        float a=(float)(tscore-fscore)/(float)fscore;

        System.out.printf("Hi, you core up%.2f%%\n", a*100); // 格式化输出

    }

}

#3 Created at ... [Delete] [Delete and Lock User]

修改下:

public class liushu {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in); // 创建Scanner对象

        System.out.print("Input your last score: "); // 打印提示

        float fscore = scanner.nextFloat(); // 读取一行输入并获取字符串

        System.out.print("Input your this score: "); // 打印提示

        float tscore = scanner.nextFloat(); // 读取2行输入并获取整数

        float a=(tscore-fscore)/fscore;

        System.out.printf("Hi, you core up%.2f%%\n", a*100); // 格式化输出

不知道

#4 Created at ... [Delete] [Delete and Lock User]
  System.out.printf("Hi, you core up%.2f%%\n", a*100); // 格式化输出

为什么在2f后面跟两个百分号呢?而不是一个呢?麻烦解答一下,有点不太明白谢谢

控心少年

#5 Created at ... [Delete] [Delete and Lock User]

因为要打印百分号,如果只有一个的话有歧义,编译器会认为是占位符,所以%%表示打印出一个%


  • 1

Reply