• [Java] 实验6參考代码


    1. 大家的.java程序都须要在一个缺省包”(default package)下编写执行提交,不要去命名新的package

        - 系统不支持package control, 亦即希望大家的java类都在缺省包下。



    2. for, if, while等。后面包括多条语句时,须要用花括号括起来


    3. Scanner对象在定义以后没有关闭。eclipse会提示一个warning. 消除这个warning的方法是在代码最后加上一句:

    public class YourClass {
      public static void main(String[] args) {
        // ..
        for (int i = 1; i <= repeat; ++ i){
          // ..
        }
        in.close(); // 在"main的结尾,main的结尾。main的结尾"加上这句
      }
    }


    更新

    这么做的原因姑且參考一份非正式的回答Closing Streams in Java

    不正式地说:

        - 对于输入流。关不关可能没什么影响

        - 对于输出流:如往磁盘文件里写数据。假设这个输入流没有被显示关闭,那么数据可能还保留在缓存(buffer, 相似内存)中,未来得及写入磁盘。那么假设这个输出流没有被关闭,那么就可能造成这些数据丢失;假设我们显式地关闭了这个输出流(close the output stream explicitly), 那么缓存中的数据将被冲刷(flushed)进磁盘,确保输出的数据不会丢失。


    40001. 求1+1/2+1/3+……+1/n

    1. 整数 / 整数 = 整数

    2. 怎样进行从1到n的循环:

    for (int i = 1; i <= n; ++ i) {
      // todo
    }

    3. 答案错误,输出4.429的同学,能够计算一下1.5 + 2.929的和。


    40004. 求1-1/2+1/3-1/4+……

    怎样在每次迭代(iteration, 非正式的能够理解成“一次循环”)中改变符号:

    flag = 1;
    for (...) {
      flag = -flag;
    }


    40007. 摄氏温度和华氏温度转换表

    1. 怎样在[x1, x2]区间内循环

    能够參考上文给出的循环例子,加以改动。

    2. 怎样在一行中输出多个数字

    能够參考之前实验的代码

    3. 请原样复制题目中的给出的输出语句


    40008. 求奇数和

    1. 在每次迭代中。怎样读到-1处停止

        1.1 能够參考我在 [Java] 实验5參考代码 -- 字母转换那题给出的for循环(不理解能够问我);或者自行回顾实验5中。是怎样解决字母转换这题的。

        1.2 也可參考以下给出完整代码:

    import java.util.Scanner;
    
    public class SumOfOdds {
      public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int repeat = in.nextInt();
        while (repeat-- != 0) {
          int sum = 0;
          // Using this for loop, we could achieve input infinite numbers,
          // until we receive an non-positive one. (if <= 0, then terminate)
          for (int num = in.nextInt(); num > 0; num = in.nextInt()) {
            // Question mark expression, which is equivalent to
            // sum = sum + num, when num is odd
            // sum = sum + 0,   when num is even
            sum += num % 2 == 1?

    num: 0; } System.out.println(sum); } } }


    40009. 求最大值

    看清题意

    看清题意

    看清题意





  • 相关阅读:
    T-SQL 数据库数据的高级查询
    数据库 T-sql 基础语句
    数据库的定义、关系型数据库的四种约束。。
    linux上使用crontab任务调度
    pip list 显示出以下错误: DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] se
    python报错:NameError: name 'converter' is not defined
    python3报错:TypeError: can't concat bytes to str
    Fiddler如何手机抓包
    数据分析----VBA的使用
    Excel进行数据分析
  • 原文地址:https://www.cnblogs.com/llguanli/p/8485137.html
Copyright © 2020-2023  润新知