Java entry study notes (4)-structured programming, data input and output, branch statements, loop statements

The system class is java.lang.System, which is a subclass of the Object class.
Its main feature is that it does not need to be initialized with the new statement before use, because when the system starts, the system class has been initialized automatically and the corresponding memory area is allocated.

1. Data input and output

In the system class java.lang.System, the following three stream objects are predefined.

  1. System.in
  2. System.out
  3. Sysstem.err

1. Standard input-System.in.read()

Used to read characters, if no characters are read, the return value is -1.

2. Scanner class

The java.util.Scanner class, which is a utility class used to scan input data, can be combined with regular expressions and retrieve specific types of data from the input stream.

In addition to using regular expressions, the Scanner class can also arbitrarily analyze strings and basic types of data. With Scanner, you can write a custom syntax analyzer for any text content to be processed.

Exercise 1 code

.nextFloat()
.nextInt()
.nextLine()
.hasNextFloat()
.hasNextInt()

package try_a_package;

import java.util.Scanner;

public class learn_3 {
    
    
	public static void main(String[] args)
	{
    
    
		float a;
		int b;
		String c;
		Scanner rd=new Scanner(System.in);
		
		a=rd.nextFloat();
		System.out.println(rd.hasNextFloat());
		b=rd.nextInt();
		System.out.println(rd.hasNextInt());
		c=rd.nextLine();
		System.out.println(c);
	}
}

operation result

Insert picture description here

Exercise 2 code

.next()

package try_a_package;

import java.util.Scanner;

public class learn_4 {
    
    
	public static void main(String args[])
	{
    
    
		String s1,s2,s3;
		Scanner rd=new Scanner(System.in);
		s1=rd.next();
		s2=rd.next();
		s3=rd.next();
		System.out.println(s1+s2+s3);
	}
}

operation result

Insert picture description here

Practice three codes

If you need to enter an integer, you need to add "gbk".
Scanner rd=new Scanner(System.in,"gbc")

package try_a_package;

import java.util.Scanner;

public class learn_4_1 {
    
    
	public static void main(String args[])
	{
    
    
		Scanner rd=new Scanner(System.in,"gbk");
		String S1;
		S1=rd.nextLine();
		System.out.println(S1);
	}
}

operation result

Insert picture description here

Two, branch statement

Simple branches will not be repeated

switch exercise

package try_a_package;

import java.util.Scanner;

public class learn_4_1 {
    
    
	public static void main(String args[])
	{
    
    
		Scanner rd=new Scanner(System.in);
		int m,s;
		double c,t;
		System.out.println("输入产品的数量:");
		m=rd.nextInt();
		s=m/500;
		switch(s)
		{
    
    
		case 0:
			c=0.01;
			break;
		case 1:
			c=0.03;
			break;
		case 2:
		case 3:
			c=0.04;
			break;
		default:
			c=0.05;
		}
		t=20*m*(1-c);
		System.out.println("购买产品的花销是:"+t);
	}
}

operation result

Insert picture description here

Three, loop statement

Exercise 1 Print a tree

package try_a_package;

import java.util.Scanner;

public class learn_4_1 {
    
    
	public static void main(String args[])
	{
    
    
		int n,i,j,p=40;
		Scanner rd=new Scanner(System.in);
		System.out.println("输入n:");
		n=rd.nextInt();
		System.out.println("输出的图形如下:");
		for(i=0;i<n;i++)
		{
    
    
			for(j=0;j<p-i;j++)
			{
    
    
				System.out.print(" ");
			}
			for(j=1;j<=2*i-1;++j)
			{
    
    
				System.out.print("*");
			}
			System.out.print("\n");
		}
	}
}

operation result

Insert picture description here

Exercise 2 Determine prime numbers

package try_a_package;

import java.util.Scanner;

public class learn_4_1 {
    
    
	public static void main(String args[])
	{
    
    
		Scanner rd=new Scanner(System.in);
		int n,i,t;
		System.out.println("输入一个正整数:");
		n=rd.nextInt();
		t=(int)Math.sqrt(n);
		for(i=2;i<=t;++i)
		{
    
    
			if(n%i==0)
			{
    
    
				break;
			}
		}
		if(i!=t+1)
		{
    
    
			System.out.println(n+"不是素数");
		}
		else
		{
    
    
			System.out.println(n+"是素数");
		}
	}
}

operation result

Insert picture description here

Insert picture description here

Exercise 3. continue statement-print prime numbers within 100

package try_a_package;

import java.util.Scanner;

public class learn_4_1 {
    
    
	public static void main(String args[])
	{
    
    
		Scanner rd=new Scanner(System.in);
		int n,i,t;
		System.out.println("100以内的素数如下:");
		
		AllPrime:
		
		for(n=2;n<100;++n)
		{
    
    
			
			t=(int)Math.sqrt(n);
			for(i=2;i<=t;++i)
			{
    
    
				if(n%i==0)
				{
    
    
					
					continue AllPrime;
				}
			}
			if(i>t)
			{
    
    
				System.out.print(n+" ");
			}
		}
		
		System.out.println("");
	}
}

operation result

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_41563270/article/details/108719287