磕程式碼c/c++/java:學生基本資訊輸入輸出

2020-08-13 11:38:46

在这里插入图片描述
在这里插入图片描述
c:

#include<stdio.h>
int main()
{
    float b,c,d;
    int a;
    scanf("%d;%f,%f,%f",&a,&b,&c,&d);
    printf("The each subject score of  No. %d is %.2f, %.2f, %.2f.\n",a,b,c,d);
    
}

注:%.f不要忘記點;輸出欄位可以複製,以免格式出錯,類似於兩個空格這種;

c++:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    float b,c,d;
    int a;
    char ch ;
    cin>>a>>ch>>b>>ch>>c>>ch>>d;
    cout << fixed;/*用小數點輸出而非科學計數法*/
    cout << "The each subject score of  No. "<<a<<setprecision(2)<<" is "<<
        b<<", "<<c<<", "<<d<<".";
}

注:cout中用setprecision表示輸出精度;cout<<fixed看註釋;用cin輸入浮點數,前面要有char;、

Java:

import java.io.*;
public class Main{
    public static void main(String[]args)throws IOException
   {
        BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
        String[]s=bf.readLine().split("[;,]");
        double a=Double.parseDouble(s[1]);
        double b=Double.parseDouble(s[2]);
        double c=Double.parseDouble(s[3]);
            System.out.printf("The each subject score of  No. %s is %.2f, %.2f, %.2f.",s[0],a,b,c);           
    }
}

注:輸出的%s;split後面的;