**题都是大一上刚开始学的c语言的难度,这些题的目的只是为了让我们学习java的基本操作。
以下仅供参考!
7-1 模仿练习
public class Main {
public static void main(String[] args) throws Exception{
System.out.println("Programming is fun!");
System.out.println("Fundamentals First");
System.out. println("Problem Driven");
}
}
7-2 Hello World!
public class Main {
public static void main(String[] args) throws Exception{
System.out.println("Hello World!");
}
}
7-3 sdut-入门-交换两个整数的值
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception{
Scanner scanner = new Scanner(System.in);
int x = scanner.nextInt();
int y = scanner.nextInt();
System.out.print(y + " ");
System.out.print(x);
}
}
**print、printf、println的区别请看:Java中print、printf、println的区别_C2G_Rg的博客-CSDN博客_print和println的区别
7-4 sdut-入门-转换字母
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception{
String a;
Scanner reader = new Scanner(System.in);
a = reader.nextLine();
System.out.print(a.toUpperCase());
reader.close();
}
}
7-5 球的表面积和体积
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception{
double r; double pi = 3.14159265;
Scanner reader = new Scanner(System.in);
r = reader.nextDouble();
double s = 4 * pi * r * r;
double v = 4 * pi * r * r * r / 3;
System.out.printf("%.2f", s);
System.out.print(" ");
System.out.printf("%.2f", v);
reader.close();
}
}
7-6 华氏温度转化为摄氏温度
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception{
double r;
Scanner reader = new Scanner(System.in);
r = reader.nextDouble();
double c = 5 * (r - 32) / 9;
System.out.printf("c=%.2f", c);
reader.close();
}
}
7-7 sdut-入门-1 A+B for Input-Output Practice(I)
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception{
int a, b, c;
Scanner reader = new Scanner(System.in);
while(reader.hasNext()){
a = reader.nextInt();
b = reader.nextInt();
c = a + b;
System.out.println(c);
}
reader.close();
}
}
7-8 sdut-入门-2 A+B for Input-Output Practice (II)
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception{
int a, b, c;
int n;
Scanner reader = new Scanner(System.in);
n = reader.nextInt();
while(n > 0){
a = reader.nextInt();
b = reader.nextInt();
c = a + b;
n--;
System.out.println(c);
}
reader.close();
}
}
7-9 sdut0-入门-3 A+B for Input-Output Practice (III)
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception{
int a, b, c;
Scanner reader = new Scanner(System.in);
while(reader.hasNext()){
a = reader.nextInt();
b = reader.nextInt();
if(a == 0 && b == 0){
break;
}
c = a + b;
System.out.println(c);
}
reader.close();
}
}
7-10 sdut-入门-4 A+B for Input-Output Practice (IV)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int n, x;
Scanner reader = new Scanner(System.in);
while(reader.hasNext()) {
int sum = 0;
n = reader.nextInt();
if(n == 0) break;
for (int i = 0; i < n; i++) {
x = reader.nextInt();
sum += x;
}
System.out.println(sum);
}
}
}
7-11 sdut-入门-5 A+B for Input-Output Practice (V)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int n, x, t;
Scanner reader = new Scanner(System.in);
t = reader.nextInt();
for(int j = 0; j < t; j++) {
int sum = 0;
n = reader.nextInt();
for (int i = 0; i < n; i++) {
x = reader.nextInt();
sum += x;
}
System.out.println(sum);
}
}
}
7-12 sdut-入门-6 A+B for Input-Output Practice (VI)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int n, x;
Scanner reader = new Scanner(System.in);
while(reader.hasNext()) {
int sum = 0;
n = reader.nextInt();
for (int i = 0; i < n; i++) {
x = reader.nextInt();
sum += x;
}
System.out.println(sum);
}
}
}
7-13 sdut-入门-7 A+B for Input-Output Practice (VII)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int a, b;
Scanner reader = new Scanner(System.in);
while(reader.hasNext()) {
int sum = 0;
a = reader.nextInt();
b = reader.nextInt();
sum = a + b;
System.out.println(sum);
System.out.printf("\n");
}
}
}
7-14 sdut-入门-8 A+B for Input-Output Practice(VIII)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int n, x, t;
Scanner reader = new Scanner(System.in);
t = reader.nextInt();
for(int j = 0; j < t; j++) {
int sum = 0;
n = reader.nextInt();
for (int i = 0; i < n; i++) {
x = reader.nextInt();
sum += x;
}
System.out.println(sum);
System.out.printf("\n");
}
}
}
7-15 打印字母B。
public class Main {
public static void main(String[] args) {
System.out.printf("*****\n");
System.out.printf("* *\n");
System.out.printf("*****\n");
System.out.printf("* *\n");
System.out.printf("*****\n");
}
}
**直接输出就行o.o 还想着怎么用for循环。。。