我在CodeGym上学Java(二)Java简介:屏幕输出、String和int类型

我在CodeGym上学Java(第二天)Java简介:屏幕输出、String和int类型

  • Java程序由类组成
  • 一个程序由一系列带有‘java’文件扩展名的文件组成,每个文件只包含一个类的代码
  • 当我们有许多类文件时,我们会将它们分组到文件夹和子文件夹中。
  • 当在代码中声明变量时,系统会为其分配一些尚未使用的内存。
    练习
    1.声明两个 int 变量,其名称为 a 和 b。在同一行中声明这些变量,并立即为它们分配不同的值。这些值可以是任意整数。
package zh.codegym.task.task01.task0109;
public class Solution {
    public static void main(String[] args) {
        //在此编写你的代码
        int a = 1;
        int b = 2;
    }
}

2.声明一个名为 name 的 String 变量。在我们声明变量的行中,立即为其赋值。在屏幕上显示变量 name。

package zh.codegym.task.task01.task0110;
public class Solution {
    public static void main(String[] args) {
        //在此编写你的代码
        String name = "1213";
        System.out.println(name);
    }
}

3.声明两个 int 变量,其名称为 a 和 b,以及一个 String 变量,其名称为 s。
在同一行中声明这些变量,立即为它们分配不同的值。

package zh.codegym.task.task01.task0111;
public class Solution {
    public static void main(String[] args) {
        //在此编写你的代码
        int a = 1;
        int b = 2;
        String s = "弟弟"; 
    }
}

4.给出变量 number。编写一个程序,显示该变量的平方:(number * number)。

package zh.codegym.task.task01.task0140;
public class Solution {
    public static int number = 25;
    public static void main(String[] args) {
        System.out.println(number * number);
    }
}

Java 语言中有两种主要类型:String 和 int。

  • 字符串/文本存储在 String 中,将整数存储在 int 中。
  • 要声明一个新变量,需要指定它的类型和名称。该名称不能与任何其他变量和/或函数的名称相同
练习

在 main 方法中,请正确放置加号和减号,使变量result等于 20。符号必须放置在声明变量result的行中。不要更改此行中变量的顺序。每个变量必须以加号或减号开头。

public class Solution {
    public static int a = 1;
    public static int b = 3;
    public static int c = 9;
    public static int d = 27;
    public static void main(String[] args) {
        int result = - a + b - c + d;
        System.out.println(result);
    }

编写一个程序,其显示内容为:“CodeGym.Learn once - use anywhere”,显示 10 次。

public class Solution {
    public static void main(String[] args) {
        //在此编写你的代码
            System.out.println("CodeGym.Learn once - use anywhere");
            System.out.println("CodeGym.Learn once - use anywhere");
            System.out.println("CodeGym.Learn once - use anywhere");
            System.out.println("CodeGym.Learn once - use anywhere");
            System.out.println("CodeGym.Learn once - use anywhere");
            System.out.println("CodeGym.Learn once - use anywhere");
            System.out.println("CodeGym.Learn once - use anywhere");
            System.out.println("CodeGym.Learn once - use anywhere");
            System.out.println("CodeGym.Learn once - use anywhere");
            System.out.println("CodeGym.Learn once - use anywhere");
    }

删除部分代码中的注释,以便屏幕上可显示“新年快乐”。

public class Solution {
    public static void main(String[] args) {
//        String s = "快乐";
//        System.out.println("圣诞节");
//        System.out.println("快乐新");
//        System.out.print("新年");
//        System.out.print("快乐新");
        System.out.print("新年");
//        System.out.println(s);
        System.out.print("快");
//        System.out.print(" ");
        System.out.println("乐");
    }

更改程序,使该变量 name 取值为“阿米戈”

public class Solution {
    public static void main(String[] args) {
        String name = "阿米戈";
        String text = "你好," + name + "!";
        System.out.println(text);
    }

更改程序以显示以下文本:用 Java 语言编写代码
使用变量。

public class Solution {
    public static void main(String[] args) {
        String s1 = "用 Java ";
        String s2 = "语言";
        String s3 = "编写代码";
        System.out.println(s1 + " " + s2 + s3);
    }

注释掉一些代码,以便屏幕上可显示“总数 = 12”。

public class Solution {
    public static void main(String[] args) {
        int a = 3;
        //int a = 10;
        int b = 6;
        //int b = 12;
        //int sum = 1 + a + b;
        //int sum = 2 + a + b;
        int sum = 3 + a + b;
       //int sum = 4 + a + b;

        System.out.println("总数 = " + sum);
    }

更改该程序,使变量 age 取值为 15

public class Solution {
    public static void main(String[] args) {
        String name = "阿米戈";
        int age = 15;
        int money = 500;
        int weight = 70;
    }

print() 和 println() 命令;

  • print() 函数用于在屏幕上逐个字符显示文本。当屏幕上某一行没有足够空间时,文本开始在下一行显示。
  • println() 函数停止在当前行上显示文本,即使该行还有显示空间。随后的文本将显示在下一行。”

练习

修复程序中的错误。该程序应显示数字 100。

public class Solution {
    public static void main(String[] args) {
        int a = 56;
        int b = 30;
        int c = 14;
        System.out.print(a + b + c);
    }

注释掉未在任何地方使用过的变量。该程序必须进行编译。

public class Solution {
    public static void main(String[] args) {
        int a = 10;
        int b = 15;
        double c = b + 38;
        //int d = a + 12;
        //double e = 12.3;
        String s = "s" + a;
        String s1 = a + "b";
        //String s2 = "a";
        String s3 = s1 + "a";
        String s4 = s3 + "b";
        System.out.println(c + s4 + s);
    }
  • 在 Java 语言中,所有代码都是方法的一部分
  • 这是在声明一个方法或一个变量:类型 + 名称。如果名称后面跟着圆括号,那么这是在声明一个新方法。如果后面没有括号,那么这是在声明一个变量

练习

在 main 方法中,编写代码使程序显示你最喜欢的诗。这首诗应该至少有 4 行。不要删除显示“我最喜欢的诗:”的那一行。

public class Solution {
    public static void main(String[] args) {
        System.out.println("我最喜欢的诗:");
        //在此编写你的代码
        System.out.println("");
        System.out.println("你站在桥上看风景");
        System.out.println("");
        System.out.println("看风景的人在楼上看你");
        System.out.println("");
        System.out.println("明月装饰了你的窗");
        System.out.println("");
        System.out.println("你装饰了别人的梦");
    }

编写一个程序,在屏幕上显示 5 的平方。

public class Solution {
    public static void main(String[] args) {
        //在此编写你的代码
        System.out.println(sqr(5));
    }

    public static int sqr(int a) {
        return a * a;
    }

编写一个程序来调用 sum 方法,而且要用参数 2 和 2 来调用该方法。

public class Solution {
    public static void main(String[] args) {
        //在此编写你的代码
        sum(2,2);
    }

    public static void sum(int a, int b) {
        int c = a + b;
        System.out.print(c);
    }
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值