面向对象程序设计JAVA学习记录(1)

发布于:2022-12-17 ⋅ 阅读:(415) ⋅ 点赞:(0)

1.What have you got?

You are requested to output what you got from the system console.

Input: A line of string, until no inut is found!  Example:
This is the first line.
This is the second line.
...
This is the end.

Output: What you get from the console(keyboard)
This is the first line.
This is the second line.
...
This is the end.

将输入的语句再打印出来,利用scanner的hasNextline判断是否还有继续输入

import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner s = new Scanner(System.in);
        while(true){
            if (s.hasNextLine()) {
                String line = s.nextLine();
                System.out.println(line);
            }
            else break;
        }
        s.close();
    }
}

2.A+B problem

You are requested to calculate the sum of two integral numbers a and b.

Input:
A pair of number seperated by the blank character. Example:
5 12

Output:  the sum of the input pair
17

import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner s = new Scanner(System.in);
        long a1 = s.nextLong();
        long a2 = s.nextLong();
        long plus = a1 + a2;
        System.out.println(plus);       
        s.close();
    }
}

 A+B problem II

You are requested to calculate the sum of two integers a and b. This time we go further. There are multiple pair of a and b in a testing group. So you need to calculate the sum several times for each testing group. Note that there may be huge number of pairs to calculate. So don't make an assumption of how many test cases.

Input:
Test case count T following by each test case. Example:
3
5 12
1 1
3 -5

Output:  the sum of each input pair
17
2
-2 

	
import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner s = new Scanner(System.in);
        int sum = s.nextInt();
        for(int i=0;i<sum;i++){
            int a1 = s.nextInt();
            int a2 = s.nextInt();
            int plus = a1 + a2;
            System.out.println(plus);
        }       
        s.close();
    }
}

 A+B Problem III

Description

Your task is to Calculate a + b.
Too easy?! Of course! I specially designed the problem for acm beginners.
You must have found that some problems have the same titles with this one, yes, all these problems were designed for the same goal.

Input

The input will consist of a series of pairs of integers a and b, separated by a space, one pair of integers per line.

Output

For each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.

Sample Input

1 5
10 20

Sample Output

6
30

需要用hasNextInt判断是否结束输出,用到了一个while循环

import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        try (Scanner s = new Scanner(System.in)) {
            while(s.hasNextInt()){
                int a = s.nextInt();
                int b = s.nextInt();
                int c = a + b;  
                System.out.println(c);
            }
        }
    }
}

 A+B Problem IV

Description

Your task is to Calculate a + b.

Input

Input contains multiple test cases. Each test case contains a pair of integers a and b, one pair of integers per line. A test case containing 0 0 terminates the input and this test case is not to be processed.

Output

For each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.

Sample Input

1 5
10 20
0 0

Sample Output

6
30

要写一个判断来识别到0 0的输入以此结束

import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        try (Scanner s = new Scanner(System.in)) {
            while(s.hasNextInt()){
                int a = s.nextInt();
                int b = s.nextInt();
                if(a==0&&b==0){
                    break;
                }
                int c = a + b;  
                System.out.println(c);
            }
        }
    }
}

3.Repeat I

Given a non negative number n(0<=n<=20) and a string s, you are expected to repeatedly output the string s n times.

Input:
4
aabb

output:
aabbaabbaabbaabb

利用scanner的nextLine来识别接下来一整行字符

import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner s = new Scanner(System.in);
        int sum = s.nextInt();
        s.nextLine();
        String str = s.nextLine();
        for(int i=0;i<sum;i++){
            System.out.print(str);
        }
        System.out.print("\n");       
        s.close();
    }
}

 Repeat II

Given a non negative number n(0<=n<=20) and a string s, you are expected to repeatedly output the string s in n lines.

Input:
4
aabb

output:
aabb
aabb
aabb
aabb

import java.util.Scanner;
public class Main
{
    public static void main(String[] args){
        Scanner s = new Scanner(System.in);
        int sum = s.nextInt();
        s.nextLine();
        String str = s.nextLine();
        for(int i=0;i<sum;i++){
            System.out.println(str);
        }       
        s.close();
    }
}

 Repeat III

An repeat problem is that given a non negative number n(0<=n<=20) and a string s, you are expected to repeatedly output the string s n times. Now it has become a bit more complicated. There are several test cases.

Input: Total test case count T following by T cases of test instances. Example:
3
4 aabb
0 a b
3 cd

output:
aabbaabbaabbaabb

cdcdcd

可利用str.substring来提取字符串片段

import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner s = new Scanner(System.in);
        int sum = s.nextInt();
        for(int i=0;i<sum;i++){
            int n = s.nextInt();
            String str = s.nextLine();
            for(int j=0;j<n;j++) {
                System.out.print(str.substring(1));
            }
            System.out.print("\n");
        }       
        s.close();
    }
}

4.Approximate PI

pi的计算

PI can be computed using the following formula:
PI = 4 * (1-1/3+1/5-1/7+1/9-...+1/n).
For a given n, write a program that displays the result of PI

Input:
A single line containing a odd number n. Example:
9

Output: the result of calculated PI, keep 6 digit after the decimal point.
3.339683

在Java中利用printf默认输出小数点后六位数

import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        double pi = 0, p = 1;
        for(int i=1;i<=n;i+=2){
            pi += (1.0/i)*p;
            p = -p;
        }   
        System.out.printf("%f\n", pi*4);    
        s.close();
    }
}

本文含有隐藏内容,请 开通VIP 后查看