JAVA入门1
A+B Problem电脑打开cmd:windows+R;输入cmd;
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);
}
}火车
Problem Description
线性王国只有一条铁路,它有n个车站,按照火车前进的方向编号1到n。在第i个车站会有Ai个乘客下车,同时会有Bi个乘客上车。火车在到达第一个车站前是空的,同样在到达最后一个车站后,车上所有的乘客都会下车,火车也会变成空的。
你的任务是计算火车的最小容量确保任何时候火车上的总人数都不会超过这个容量。注意,在每个车站,上车的乘客都是在下车乘客都下来之后才开始上的。
Input
多组输入,第一行是一个正整数T,代表组数。
每组输入数据第一行只有一个整数n (2 <= n <= 1000) ——车站的总数。
接下来是n行,每行有两个整数Ai和Bi(0 <= Ai,Bi <= 1000)——分别代表在第i个车站下车和上车的乘客数。车站是沿照火车前进方向从起点到终点按顺序给出的。
注意起点站不会有人下车,终点站不会有人上车。
Output
每组输出只有一个整数代表火车的最小容量(可以是0)。每组输出之后输出一个空行。
Sample Input
1
4
0 3
2 5
4 2
4 0
Sample Output
6
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 ;
A=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=A-a+b;
}
int max=A;
for(int k=2;k<=n;k++)
{
if(A>max)
max=A;
}
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);
}
}
}
文档来源:51CTO技术博客https://blog.51cto.com/u_15317888/3231860
页:
[1]