import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
int a, b;
a = reader.nextInt();
b = reader.nextInt();
System.out.println(a + b);
reader.close();
}
}
import java.util.Scanner;指的是导入Scanner 类,导入后才能创建一个对象:
Scanner reader = new Scanner(System.in);也就是创建这个对象。然后才能用对象来获取输入的信息。即以下内容:a = reader.nextInt();
b = reader.nextInt(); 本代码 创建的Scanner 类名称叫做 reader ,所以输入的时候也要用reader,如果是其他的也可以替换。
A+B for Input-Output Practice (I)
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
while(sc.hasNext())
{
int a=sc.nextInt();
int b=sc.nextInt();
System.out.println(a+b);
}
}
}
A+B for Input-Output Practice (II)
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
for(int i=0;i<n;i++)
{
int a=sc.nextInt();
int b=sc.nextInt();
System.out.println(a+b);
}
}
}
A+B for Input-Output Practice (III)
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
while(sc.hasNext())
{
int a=sc.nextInt();
int b=sc.nextInt();
if(a==0&&b==0)break;
else
System.out.println(a+b);
}
}
}
A+B for Input-Output Practice (IV)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int n=sc.nextInt();
if(n==0)break;
int sum=0;
for(int i=0;i<n;i++)
{
int x=sc.nextInt();
sum=sum+x;
}
System.out.println(sum);
}
}
}
A+B for Input-Output Practice (V)
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
int n=scanner.nextInt();
for(int j=0;j<n;j++)
{
int m=scanner.nextInt();
int sum=0;
for(int i=0;i<m;i++)
{
int x=scanner.nextInt();
sum=sum+x;
}
System .out.println(sum);
}
}
}
A+B for Input-Output Practice (VI)
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
while(scanner.hasNext())
{
int m=scanner.nextInt();
int sum=0;
for(int i=0;i<m;i++)
{
int x=scanner.nextInt();
sum=sum+x;
}
System .out.println(sum);
}
}
}
A+B for Input-Output Practice (VII)
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
while(scanner.hasNext())
{
int a=scanner.nextInt();
int b=scanner.nextInt();
System.out.println(a+b);
System.out.println();
}
}
}
A+B for Input-Output Practice
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
int n=scanner.nextInt();
for(int j=0;j<n;j++)
{
int m=scanner.nextInt();
int sum=0;
for(int i=0;i<m;i++)
{
int x=scanner.nextInt();
sum=sum+x;
}
System .out.println(sum);
System.out.println();
}
}
}
C语言实验——Hello World!(printf练习
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
System .out.println("Hello World!");
}
}
C语言实验——格式化输出(常量练习)
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
System .out.println("100");
System .out.println("A");
System .out.println("3.140000");
}
}
C语言实验——图形输出(字符常量练习)
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
for(int i=1;i<=6;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print("#");
}
System.out.println();
}
}
}
C语言实验——单个字符输入和输出(顺序结构)
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
char c;
String s;
Scanner input = new Scanner(System.in);
s = input.next();
c = s.charAt(0);
System.out.println(c);
}
}
C语言实验——计算A+B(顺序结构)
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner reader =new Scanner(System.in);
int a=reader.nextInt();
int b=reader.nextInt();
System.out.println(a+b);
}
}
C语言实验——交换两个整数的值(顺序结构)
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner reader =new Scanner(System.in);
int x=reader.nextInt();
int y=reader.nextInt();
int z;
z=x;x=y;y=z;
System.out.print(x+" "+y);
}
}
C语言实验——转换字母(顺序结构)
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
char c;String s;
Scanner input =new Scanner(System.in);
s=input.next();
c=s.charAt(0);
char a=(char)(c-32);
System.out.print(a);
}
}
C语言实验——输出字符串
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
System.out.print("This is a C program.");
}
}
C语言实验——求两个整数之和
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
int a=123;int b=456;
int c=a+b;
System.out.print("sum is "+c);
}
}
C语言实验——打印图形
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
for(int i=1;i<=30;i++)
{
System.out.print("*");
}
System.out.println();
System.out.println();
for(int j=1;j<=10;j++)
{
System.out.print(" ");
}
System.out.println("Very good!");
System.out.println();
for(int i=1;i<=30;i++)
{
System.out.print("*");
}
System.out.println("");
}
}
C语言实验——用*号输出字母C的图案
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
System.out.print("*****"+"\n"+"*"+"\n"+"*"+"\n"+"*"+"\n"+"*****");
}
}
C语言实验——三个整数和、积与平均值
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner reader = new Scanner(System.in);
int a=reader.nextInt();
int b=reader.nextInt();
int c=reader.nextInt();
int sum=a+b+c;
int dun=a*b*c;
double ave=sum/3.0;
System.out.printf("%d %d %.2f\n",sum,dun,ave);
}
}
C语言实验——逆置正整数
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner reader = new Scanner(System.in);
int x=reader.nextInt();
int a=x%10;
int b=x/10%10;
int c=x/10/10;
int y=a*100+b*10+c;
System.out.println(y);
}
}
C语言实验——买糖果
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner reader = new Scanner(System.in);
int x=reader.nextInt();
x=x*10;
int a=x/3;
int b=x%3;
System.out.println(a+" "+b);
}
}
C语言实验——圆柱体计算
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner reader = new Scanner(System.in);
int r=reader.nextInt();
int h=reader.nextInt();
double zhouchang=2*3.1415926*r;
double mianji=3.1415926*r*r;
double cemian=zhouchang*h;
double V=mianji*h;
System.out.printf("%.2f %.2f %.2f %.2f\n",zhouchang,mianji,cemian,V);
}
}
C语言实验——温度转换
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner reader = new Scanner(System.in);
double F=reader.nextDouble();
double C=5*(F-32)/9;
System.out.printf("%.2f\n",C);
}
}
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner reader = new Scanner(System.in);
int T=reader.nextInt();
int A[];
A=new int [1001];
A[0]=0;
for(int i=1;i<=T;i++)
{
int n=reader.nextInt();
for(int j=1;j<=n;j++)
{
int a=reader.nextInt();
int b=reader.nextInt();
A[j]=A[j-1]-a+b;
}
int max=A[1];
for(int k=2;k<=n;k++)
{
if(A[k]>max)
max=A[k];
}
System.out.println(max);
System.out.println();
}
}
}
压岁钱
import java.util.Scanner;
import javafx.application.Preloader.StateChangeNotification;
public class Main
{
public static void main(String[] args)
{
Scanner reader = new Scanner(System.in);
int T=reader.nextInt();
for(int i=0;i<T;i++)
{
int a=reader.nextInt();
if(1000000%a==0)
System.out.println(1000000/a);
else
System.out.println("No");
}
}
}
直角坐标系
import java.util.Scanner;
import javafx.application.Preloader.StateChangeNotification;
public class Main
{
public static void main(String[] args)
{
Scanner reader = new Scanner(System.in);
while(reader.hasNext())
{
int x=reader.nextInt();
int y=reader.nextInt();
if(x>0&&y>0)System.out.println("1");
if(x<0&&y>0)System.out.println("2");
if(x<0&&y<0)System.out.println("3");
if(x>0&&y<0)System.out.println("4");
}
}
}
蝴蝶效应
数字和
import java.util.Scanner;
import javafx.application.Preloader.StateChangeNotification;
public class Main
{
public static void main(String[] args)
{
Scanner reader = new Scanner(System.in);
while(reader.hasNext())
{
int n=reader.nextInt();
int sum=0;
for(int i=0;i<n;i++)
{
int x=reader.nextInt();
if(x%2!=0)sum=sum+x;
}
System.out.println(sum);
}
}
}
得分
import java.util.Scanner;
import javafx.application.Preloader.StateChangeNotification;
public class Main
{
public static void main(String[] args)
{
Scanner reader = new Scanner(System.in);
while(reader.hasNext())
{
int a=reader.nextInt();
int b=reader.nextInt();
int c=reader.nextInt();
int sum=3*a+2*b+c;
System.out.println(sum);
}
}
}
神奇的细胞
import java.util.Scanner;
import javafx.application.Preloader.StateChangeNotification;
public class Main
{
public static void main(String[] args)
{
Scanner reader = new Scanner(System.in);
int n=reader.nextInt();
int m=1;
for(int i=1;i<n;i++)
{
m=m*2;
}
System.out.println(m);
}
}
学区房问题
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner reader = new Scanner(System.in);
while(reader.hasNext())
{
int s1=reader.nextInt();
int s2=reader.nextInt();
int T1=reader.nextInt();
int T2=reader.nextInt();
int a=s2*T2-s1*T1;
if(a<0)System.out.println("0");
else System.out.println(a);
}
}
}