Java 之 实验二 -- 数据与表达式

实验二

数据与表达式

打印一首诗

  编写一段Java程序,打印如下信息:“Roses are red”。程序应该包含一个main方法(参照例子Dreams.java。注意一下事项:
   类名必须与文件名相匹配(不包括扩展名.java)
   main方法的定义必须位于类内部(在第一个“{”和最后一个“}”之间)
  打印信息的语句必须位于main方法内
  添加必要的注释:程序文件名称;主要功能介绍;程序中主要语句的解释。
 Roses are red,Violets are blue,Sugar is sweet,And so are you!
  编译并运行程序,输出结果如下:
   Roses are red
   Violets are blue
   Sugar is sweet
   And so are you!

public class Main {
    public static void main(String[] args) {
        System.out.println("Roses are red");
        System.out.println("Violets are blue");
        System.out.println("Sugar is sweet");
        System.out.println("And so are you!");
    }
}

打印学生名单

  开发一段程序,用于打印学生姓名和其他信息的一个列表。使用转义符“\t”有助于安排输出信息的格式。请先阅读下面给出的程序Names.java。

   1. 将Names.java保存至本地文件夹。编译并运行,查看结果。
   2.添加你本人和至少另外两位同学的姓名和籍贯至程序中。保存、编译并运行,查看结果。保证输出的结果行列之间对齐。
   3. 修改程序,添加一个第3列“Major”至程序中。假设Sally的专业是Computer Science,Alexander的专业是Math。注意第3列的表头是“Major”,以及行列对其(要使用“\t”转义符)。

//************************************************
// Names.java
//
//  Prints a list of student names with their hometowns
// and intended major
//************************************************
public class Names {
    //-----------------------
    //main prints the list
    //-----------------------
    public static void main(String[] args){
        System.out.println(' ');
        System.out.println("\tName\t\tHometown\t\tMajor");
        System.out.println("\t====\t\t========\t\t=====");
        System.out.println("\tLiuKaiYang\tLongYan\t\t\tMath");
        System.out.println("\tZhangSan\tBeiJing\t\t\tMath");
        System.out.println("\tLiSi\t\tShangHai\t\tMath");
        System.out.println("\tSally\t\tRoanoke\t\t\tComputer Science");
        System.out.println("\tAlexander\tWashington\t\tMath");
        System.out.println(' ');
    }
}

加号(“+”)的两个作用

  在Java中,加号(“+”)可以用于数字相加,或者字符串的连接。当使用字符串时,应保证字符串的所有内容在同一行。下面是一个错误的例子:
   System.out.println(“It is NOT okay to go to the next line in a Long String!!!”);
  如果字符串过长,解决的办法是将长字符串拆分成两个或多个短字符串,以加号连接。下面是一个正确的例子:
   System.out.println(“It is Okay to break a long string into” + “parts and join them with a + sympol.”);
  因此,当加号用于字符串之间时,表示将两个字符串相连。但是,当其用于两个数字之间时,就表示数学上的相加。

  1. 仔细阅读PlusTest.java文件,观察加号在不同设置下的行为表现:
   a. 阅读文件PlusTest.java文件中的源程序
   b. 将PlusTest.java保存至个人目录。
   c. 编译并运行程序。将最后面三行语句的输出结果记录下来,注意以下要点。
  当“+”两边都是操作数的时候,该符号被看做“加号”。
  当“+”两边至少有一个字符串的时候,该符号被看做连接符。
  如果一个表达式中包含了不只一个“+”,那么括号中的表达式具有优先计算顺序。如果没有括号,加号的计算顺序是从左至右。

  2. 使用“+”编写一个Java程序,输出结果如下所示:
Ten robins plus 13 canaries is 23 birds.
要求:程序只能使用一条调用println方法的语句。必须使用“+”来实现加法操作和字符串连接。

//**********************************************************
//  PlusTest.java
//
//  Demonstrate the different behaviors of the + opertor
//***********************************************************

public class PlusTest {
    //--------------------------------------------------
    //main prints sone expressions using the + opertor
    //--------------------------------------------------
    public static void main(String[] args){
        System.out.println("This is a long string that is the " +
         "concatenation of two sherter strings.");
        System.out.println("The first conputer was invented about " + 55 + " years ago.");
        System.out.println("8 Plus 5 is " + 8 + 5);
        System.out.println("8Plues 5 is " + (8+5));
        System.out.println(8 + 5 + "equals 8 plus 5.");
    }
}
public class PlusSign {
    public static void main(String[] args){
        System.out.println("Ten robins plus "+13+" canaries is "+23+" birds.");
    }
}

表达式计算

public class Arithmetic {
    public static void main(String[] args){
        int a = 3 , b = 10 , c = 7;
        double w = 12.9 , y = 3.2;
        System.out.println("a. a+b*c= " + (a+b*c));
        System.out.println("b. a-b-c= " + (a-b-c));
        System.out.println("c. a/b= " + (a/b));
        System.out.println("d. b/a= " + (b/a));
        System.out.println("e. a-b/c= " + (a-b/c));
        System.out.println("f. w/y= " + (w/y));
        System.out.println("g. y/w= " + (y/w));
        System.out.println("h. a+w/b= " + (a+w/b));
        System.out.println("i. a%b/y= " + (a%b/y));
        System.out.println("j. b%a= " + (b%a));
        System.out.println("k. w%y= " + (w%y));
    }
}

计算圆面积与周长

注意事项:
  Main方法里面的前3行语句声明了PI,半径radius和面积area。注意每个标识符的数据类型:PI是final double,因为它是一个浮点型常量;半径是整型int变量;面积是double型变量。
  前3行语句给三个变量赋值,因此也就将它们实例化。也可以采取其他方法处理,比如将声明和实例化分开处理,相比之下,不如前者简洁。
  接下来的一行用于输出一条语句,显示给定半径的圆面积。
  在下面一行语句是一条赋值语句,将半径值设定为20。注意这并不是一个声明,所以此处并未出现int。我们使用同一内存位置来存储半径的值20和10,重新赋值的过程并没有分配新的内存空间。
  与此相同,再下一行的赋值语句也不会出现double。
  最后输出的程序结果输出以新半径值计算的圆面积。
  保存文件Circle.java至本地目录,按照以下要求修改该程序:
   1. 按照圆的周长公式。在程序中增加语句,以计算圆周长。按照以下步骤进行:
    声明一个新变量,用以存储周长值;
   每次计算周长值之后,将结果保存至该变量;
   另外增加打印语句,打印你的结果。
  2. 如果半径值翻倍,那么圆的周长和面积会发生怎样的变化?改写上面的程序,输出以下内容:1)原始半径值条件下的周长和面积;2)半径增大一倍后圆的周长和面积;3)半径变化前后两个周长的比值,以及两个面积的比值。请改写本程序,重新声明变量,即两个周长,两个面积。按照以下步骤进行:
   改变原程序中面积和周长变量的名称,以便于区分新声明的变量名称;
   在程序最后面,计算半径改变前后两个周长的比值,以及两个面积的比值,输出结果;

  3. 上面的程序给出了在半径值为10和20的情况下,圆周长和面积的计算结果。这是属于一种硬编码(Hardcoded)的形式。为了使程序更为灵活,可以计算任何半径值情况下的周长和面积值,请尝试改写程序。按照以下步骤进行:
  在文件的顶端,增加以下语句:
   import java.util.Scanner
  这行程序告诉编译器程序将使用util包中的Scanner类。在主方法中,创建Scanner的对象scan,用于读取来自于System.in的数据。
  声明radius变量,但并不赋值。增加两行语句用于从用户端读取半径值:
  输出一行提示符,告诉用户将要从键盘读取数据,比如:Please enter a value for the radius!
  一条实际读取输入流信息的语句。我们已经假设半径数据类型为整型,因此使用Scanner类的nextInt()方法来读取输入值。

import java.util.Scanner;
/********************************************************
 Circle.  javaPrint the area of a circle with two different radii
/**********************************************************/
public class Circle {
    public static void main (String[] args){

        System.out.println("Please enter a value for the radius!");
        Scanner scan = new Scanner(System.in);
        final double PI = 3.14159;

        // int raw_radius = 10;
        // double raw_area = PI * raw_radius * raw_radius;
        // double raw_perimeter = 2 * PI * raw_radius;10

        // System.out.println("The area of a circle with radius " + raw_radius + " is " + raw_area);
        // System.out.println("The perimeter of a circle with radius " + raw_radius + " is " + raw_perimeter);

        // int radius = 20;
        int radius = scan.nextInt();
        double area = PI * radius * radius;
        double perimeter = 2 * PI * radius;

        System.out.println("I double the radius. The area of a circle with radius " + radius + " is " + area);
        System.out.println("I double the radius. The perimeter of a circle with radius " + radius + " is " + perimeter);

        // System.out.println("The ratio of two circumferences: " + (raw_area/area));
        // System.out.println("The ratio of two areas: " + (raw_perimeter/perimeter));
        scan.close();
    }
}

END

posted @ 2023-09-18 17:47  Ivan丶ky  阅读(246)  评论(0)    收藏  举报