实验习题 Java 1.1

Java, 模仿练习, 输出三行信息

问题描述

模仿练习, 编写程序,输出三行信息

Write the program to display three messages.

要求:

请不要复制参考代码,在开发工具上手工录入代码,运行正确后,在OJ上提交代码;

参考代码

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");
   
}
}

 

假如输入

没有输入

应当输出

Programming is fun!

Fundamentals First

Problem Driven

--------------------题目分割线--------------------

Java, Displays table

参考答案

public class Main {
   
public static void main(String[] args) {
        System.
out.println("a      a^2    a^3");
       
System.out.println("1      1      1");
       
System.out.println("2      4      8");
       
System.out.println("3      9      27");
       
System.out.println("4      16     64");
   
}
}

 --------------------题目分割线--------------------

Java, 模仿练习,计算并输出算式的值

模仿练习,计算并输出算式的值

Write a program that displays the result of

(10.5 + 2 * 3)/(45 - 3.5).

 

要求:

请不要复制参考代码,在开发工具上手工录入代码,运行正确后,在OJ上提交代码;

参考代码

public class Main {
   
public static void main(String[] args) throws Exception {
        System.
out.println( (10.5 + 2 * 3) / (45 - 3.5) );
   
}
}

 

输入

输出

算式的值

 

样例输入

 

样例输出

0.39759036144578314

 --------------------题目分割线--------------------

Java, Displays the result of PI

参考答案

public class Main {
   
public static void main(String[] args) throws Exception {
        System.
out.println(4 * (1 - 1.0 / 3 + 1.0 / 5 - 1.0 / 7 +
               
1.0 / 9 - 1.0 / 11 + 1.0 / 13));
   
}
}

 --------------------题目分割线--------------------

Java, 计算摄氏温度

参考代码

public class Main {
   
public static void main(String[] args) throws Exception {
        System.
out.printf("100 %.2f",5 * (100.0 - 32) / 9);
   
}
}

 --------------------题目分割线--------------------

Java,模仿练习, 计算两个数的和

计算两个数的和

通过键盘为变量ab赋值,然后计算变量ab的和,并将和赋值给变量sum,最终输出变量sum的值;

要求:

请不要复制代码,在开发工具上手工录入代码,运行正确后,在OJ上提交代码;

 

注意:

未经题目允许,输出提示性语句如“请输入”、"please input"之类OJ系统一律判为错误答案。

请仔细观察 空格、回车等符号数量和位置,输出时,不符合题目要求,一律判为格式错误。

 

题目描述:计算两个数的和

输入要求:输入两个整数

输出要求:两个数的和

 

假如输入

2 8

应当输出

10

 

参考代码:

import java.util.Scanner;
public class
Main {
   
public static void main(String[] args) throws Exception {
        Scanner myScan =
new Scanner(System.in);
        int
a = myScan.nextInt();
        int
b  = myScan.nextInt();
       
System.out.println(a+b);
   
}
}

=====

Java,计算两个数的乘积

参考代码
import java.util.*;
public class
Main {
   
public static void main(String[] agrs) throws Exception {
        Scanner myScan =
new Scanner(System.in);
        int
x = myScan.nextInt();
        int
y = myScan.nextInt();
       
System.out.println(x*y);
   
}
}

=====

 

Java之输入输出处理

由于ACM竞赛题目的输入数据和输出数据一般有多组(不定),并且格式多种多样,所以,如何处理题目的输入输出是对大家的一项最基本的要求。这也是困扰初学者的一大问题。

1. 输入:

格式1Scanner sc = new Scanner (new BufferedInputStream(System.in));

格式2Scanner sc = new Scanner (System.in);

在读入数据量大的情况下,格式1的速度会快些。

读一个整数: int n = sc.nextInt(); 相当于 scanf("%d", &n); cin >> n;

读一个字符串:String s = sc.next(); 相当于 scanf("%s", s); cin >> s;

读一个浮点数:double t = sc.nextDouble(); 相当于 scanf("%lf", &t); cin >> t;

读一整行: String s = sc.nextLine(); 相当于 gets(s); cin.getline(...);

判断是否有下一个输入可以用sc.hasNext()sc.hasNextInt()sc.hasNextDouble()sc.hasNextLine()

1:读入整数

Input  输入数据有多组,每组占一行,由一个整数组成。

Sample Input

56

67

100

123

参考代码

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) { //判断是否结束
int score = sc.nextInt();//读入整数
// ...
}
}
}





posted @ 2015-05-06 09:48  jlxuqiang  阅读(1378)  评论(0编辑  收藏  举报