JAVA学习二
Posted zhichun
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVA学习二相关的知识,希望对你有一定的参考价值。
一、控制流程
块的作用:(块)是指由一对花括号括起来的若干java语句。块确定了JAVA的作用域。
public static void main(String[] args)
int n;
...
int k;
//k is only defined up to here
比如下列声明就是错误的:
public static void main(String[] args)
int n;
...
int k;
int n;//ERROR-can‘t redefine in inner blcok
二、循环
package demo10;
import java.io.Console;
import java.util.*;
public class java1
public static void main(String[] args)
Scanner in = new Scanner(System.in);
System.out.println("How much money do you need to retire?");
double goal = in.nextDouble();
System.out.println("How much money will you contribute every day?");
double payment = in.nextDouble();
System.out.println("Interest rate in %");
double interestRate = in.nextDouble();
double balance = 0;
int years = 0;
while(balance<goal)
balance+=payment;
double interest = balance * interestRate/100;
balance += interest;
years++;
System.out.println("You can retire in" + years +"years");
三、多重选择:SWITCH语句
Scanner in = new Scanner(System.in);
System.out.println("Select option(1,2,3,4)");
int choice = in.nextInt();
switch(choice)
case 1:
///
break;
case 2:
///
break;
case 3:
///
break;
default:
//bad input
break;
switch 语句将从与选项值相匹配的 case 标签处开始执行直到??到 break 语句,或者执行 到 switch 语句的结束处为止。如果没有相匹配的 case 标签,而有 default 子句,就执行这个 子句。
四、大数值
java.math包中又两个很有用的类:BigInteger 和 BigDecimal。这两个类可以处理包含任意长度数字序列的数值
BigInteger a = BigInteger.valueOf(100);
不能使用算数运算符处理大数值。
BigInteger c = a.add(b);
BigInteger d = c.multiply(b.add(BigInteger.valueOf(2)))
API java.math.BigInteger
BigInteger add(BigInteger other)
BigInteger subtract(BigInteger other)
BigInteger multiply(BigInteger other)
BigInteger divide(BigInteger other)
BigInteger mod(BigInteger other)
返回这个大整数和另一个大整数other的和、差、积、商以及余数
以上是关于JAVA学习二的主要内容,如果未能解决你的问题,请参考以下文章