基础Java语句

作者: 张一毛1997 | 来源:发表于2018-06-22 22:51 被阅读3次

Patterns

Sum 语句

Goal: Find the sum of a collection of items.

<type> sum = 0;
<for each item>
{
    sum += <item>;
}

Example:

int sum = 0;
for(i = 0; i < 10; i++) {
    sum += i;
}

Output 语句

Goal: Show a value to the user.

System.out.println("<Label>" + <Value>);

Example:

System.out.println("Age is" + age);
System.out.println("Name is" + name);

Read 语句

Goal: Read a value from the user.

System.out.print("<prompt>");
<type> <varible> = <read operation>;

or,

System.out.print("<prompt>");
<type> <varible> = <read operation>; // if varible has already been declared.

Example:

System.our.print("Age: ");
int age = In.nextInt();

Class.In:

public static int nextInt()    // Read a Integer.
public static double nextDouble()    // Read a Double
public static char nextChar()    // Read a char
public static String nextLine()    // Read String

Read loop 语句

Goal: Read values until the user enters an "end of input" value.

<Read pattern>
while(<value> != <end value>) {
    <use the value>
    <read pattern>
}

Example:

int sum;
int i = In.nextInt();
while(i != -1) {
    sum += i;
    int i = In.nextInt();
}

Array loop 语句

Goal: Loop over items in an array.

for(int i = 0; i < <array>.length; i++>) {
    <use the item array[i]>
}

Example:

int[] list = new int[5];
for(int i; i < list.length; i++>) {
    System.out.println(list[i]);
}

Count 语句

Goal:
without guard: Count the number of items in a collection.
with guard: Count the number of items that satisfy a collection.

// Without guard
int count = 0;
<for each item>

// With guard:
int count = 0;
<for each item>
    if(<guard>)
        count++;

Example:

// Without guard
String[] names = {Adam, Bob, Christina, David, Eva};
int count = 0;
for(i = 0; i < names.length; i++>)
    count++;

// With guard:
String[] names = {Adam, Bob, Christina, David, Eva};
int count = 0;
for(i = 0; i < names.length; i++>)
    if(names[i].length <= 4>)
        count++;

相关文章

  • Java学习

    1 java语言基础 java语言简介, Java开发环境, 变量, 运算符, 表达式, 分支语句, 循环语句, ...

  • Java 基础02Java编程基础

    Java 基础01Java开发入门 Java基础语法 Java代码的基本格式:Java程序中代码分为结构定义语句和...

  • JAVA-跳转语句(break,continue)

    java-编程基础 1. 跳转语句(break,continue) 1.1 break语句 break语句在swi...

  • 4/07day28_js基础

    day28_js基础 回顾 JS基础语法 JS运算符 JS流程控制语句 条件语句 JS的条件语句和Java语法基本...

  • 基础Java语句

    Patterns Sum 语句 Goal: Find the sum of a collection of ite...

  • Java编程基础(5)

    Java基础知识-控制流程方法(2):1.foreach 循环语句 2.return 语句 3.break 语句 ...

  • java基础,每天都应该复习

    java基础,每天都应该复习 1.Java语言基础(选择结构switch语句的格式及其解释)(掌握) A:swit...

  • java基础,每天都应该复习

    1.Java语言基础(选择结构switch语句的格式及其解释)(掌握) A:switch语句的格式 switch(...

  • java历程1

    今天第一天参加正式笔试。失败。 递归。 五星数JAVA运算。 JAVA工厂模式。 SQL语句基础语句。 SQL多表查询。

  • Java基础之语句

    此为个人学习笔记,如有错误,欢迎指教 七、语句 7.1 概述: 7.2 选择结构: 7.2.1 if语句: if语...

网友评论

    本文标题:基础Java语句

    本文链接:https://www.haomeiwen.com/subject/hupqyftx.html