/**
开封网站建设公司成都创新互联,开封网站设计制作,有大型网站制作公司丰富经验。已为开封上千家提供企业网站建设服务。企业网站搭建\外贸网站制作要多少钱,请找那个售后服务好的开封做网站的公司定做!
* 懒汉模式
* */
public class Singleton {
private volatile static Singleton uniqueInstance;
private Singleton(){}
public static Singleton getInstance(){
if(uniqueInstance==null){ //首先判断是否已创建,如果未创建,才进入同步代码块
//这样可以减少同步代码块带来的内存开销
synchronized(Singleton.class){ //synchronized将该类锁住,使得其他线程无法访问
if(uniqueInstance==null){ //再次检查是否创建过实例
uniqueInstance=new Singleton();
}
}
}
return uniqueInstance;
}
}
/**
* 饿汉模式
* */
public class Singleton {
private static Singleton uniqueInstance=new Singleton() ;
private Singleton(){}
public static Singleton getInstance(){
return uniqueInstance;
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class AccountDemo {
public static double MONEY = 0;// 初始化金额是100元。
public static void main(String[] args) {
final String USER_NAME = "zhangsan";// 用户名
final String PASSWORD = "123456";// 密码
while (true) {
System.out.print("请输入用户名:");
String user_name = getString();
System.out.print("请输入密码:");
String password = getString();
if (user_name != null user_name.equals(USER_NAME)
password != null password.equals(PASSWORD)) {
System.out.println("登陆成功!你要干什么?");
while (true) {
System.out.println("1:存款");
System.out.println("2:取款");
System.out.println("3:查询余额");
System.out.println("q:退出程序");
System.out.print("请选择:");
String userIn = getString();
int in = 0;
if (userIn != null userIn.equals("1")) {
in = Integer.parseInt(userIn);
} else if (userIn != null userIn.equals("2")) {
in = Integer.parseInt(userIn);
} else if (userIn != null userIn.equals("3")) {
in = Integer.parseInt(userIn);
} else if (userIn != null
userIn.trim().toUpperCase().equals("Q")) {
in = 4;
} else {
System.out.println("你输入的指令不正确!请重新输入。");
continue;
}
switch (in) {
case 1:
double add_money = 0;
while (true) {
System.out.print("请输入你要存入的金额:");
try {
add_money = Double.parseDouble(getString());
} catch (Exception e) {
System.out.println("金额输入不正确!");
continue;
}
break;
}
MONEY += add_money;
System.out.println("存入的金额是" + add_money
+ "\r\n请选择你要的操作:");
break;
case 2:
double money = 0;
while (true) {
System.out.print("请输入你要取出的金额:");
try {
money = Double.parseDouble(getString());
} catch (Exception e) {
System.out.println("金额输入不正确!");
continue;
}
if (money MONEY) {
System.out.println("取出的金额大于现有存款,请重新输入要取出的金额!");
continue;
}
break;
}
MONEY -= money;
System.out.println("取出的金额是" + money + "\r\n请选择你要的操作:");
break;
case 3:
System.out.println("你的余额是:" + MONEY + "\r\n请选择你要的操作:");
break;
case 4:
System.out.println("程序退出!");
return;
}
}
} else {
System.out.println("错误:用户名与密码不匹配!\r\n");
System.out.println("按任意键:重新输入用户名和密码。");
System.out.println("q:退出程序。");
System.out.print("请选择:");
String in = getString();
if (in.trim().toUpperCase().equals("Q")) {
break;
}
}
}
}
public static String getString() {
String str = null;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
//第一题的答案:
import java.util.Scanner;
public class test {
public static void main(String[] args)
{
System.out.println("输入购买金额:");
Scanner input=new Scanner(System.in);
double a=input.nextDouble();
System.out.println("输入顾客类型(会员或普通):");
String b=input.next();
if(b=="会员")
{
if(a=100)
{
a=a*0.8;
System.out.println("需付款:"+a);
}
else
{
System.out.println("需付款:"+a);
}
}
if(b=="普通")
{
if(a=200)
{
a=a*0.75;
System.out.println("需付款:"+a);
}
else
{
System.out.println("需付款:"+a);
}
}
}
}
//下面是第二题答案:
public class test {
public static void main(String[] args)
{
for(int i = 0; i 3; i++)
{
for(int x = i + 1; x 3; x++)
{
System.out.print(" ");
}
for(int y = 0; y (i + 1) * 2 - 1; y++)
{
System.out.print("*");
}
System.out.println();
}
for(int i = 0; i 4; i++)
{
for(int x = 0; x i; x++)
{
System.out.print(" ");
}
for(int y = i; y 2 * 4 - i - 1; y++)
{
System.out.print("*");
}
System.out.println();
}
}
}