学习练习
作业: 1,输入三个整数,输出他们的最大值
package com.qianfeng.day03;
import java.util.Scanner;
public class Work01 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int a,b,c;
int max;
System.out.print("请您输入三个整数:");
a = input.nextInt();
b = input.nextInt();
c = input.nextInt();
max = a;
if(a > b) {
max = a;
} else {
max = b;
}
if(c > max) {
max = c;
}
System.out.println("最大值为:" + max);
input.close();
}
}
2、设计一个超市收银程序,输入购买商品总价,并根据总价进行优惠,策略是这样的:100O以上打6折800-1000之间打7折500-800之间打8折300-500之间打9折30O以下原价付款,显示应付款,然后再输入实际付款,最后显示找零的结果
package com.qianfeng.day03;
import java.util.Scanner;
public class Work02 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double commodityPrice;
double practicalPrice;
double payment;
double change;
double discount;
System.out.print("商品总价(¥):");
commodityPrice = input.nextDouble();
if (commodityPrice >= 1000) {
discount = 0.6;
} else if(commodityPrice >= 800) {
discount = 0.7;
} else if(commodityPrice >= 500) {
discount = 0.8;
} else if(commodityPrice >= 300) {
discount = 0.9;
} else {
discount = 1;
}
practicalPrice = commodityPrice * discount;
System.out.println("为您打" + (int)(discount*10) + "折");
System.out.println("应付款:" + practicalPrice + "¥");
System.out.print("请付款(¥):");
payment = input.nextDouble();
change = (payment*10 - practicalPrice*10) / 10;
System.out.println("找您零钱:" + change + "¥");
input.close();
}
}
3、使用嵌套if结构实现如下程序︰单价188,输入年龄,70岁以上免费 30-70之间85折18-30原价18以下5折,输出最终的价格
package com.qianfeng.day03;
import java.util.Scanner;
public class Work03 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double price = 188;
double discount;
double practicalPrice;
int age;
System.out.print("现在单价为:" + price + "¥" + "请您输入您的年龄:");
age = input.nextInt();
if(age >= 30) {
if(age < 70) {
discount = 0.85;
} else {
discount = 0;
}
} else {
if(age >= 18) {
discount = 1;
} else {
discount = 0.5;
}
}
practicalPrice = price * (discount*100) / 100;
System.out.println("您的价格为单价的" + (int)(discount*100) + "%");
System.out.println("最终价格为:" + practicalPrice + "¥");
System.out.println();
input.close();
}
}
LeetCode练习
- 回文数
给你一个整数 x ,如果 x 是一个回文整数,返回 true ;否则,返回 false 。 回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。例如,121 是回文,而 123 不是。
package com.qianfeng.day03;
import java.util.Scanner;
public class Test02 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int x = input.nextInt();
boolean result = isPalindrome(x);
System.out.println(result);
input.close();
}
public static boolean isPalindrome(int x) {
boolean result=true;
int[] number = new int[11];
int is = 0;
if (x < 0) {
result = false;
}
for (int i = 0; i < number.length; i++) {
number[i] = x%10;
x = x/10;
if(x == 0) {
is = i+1;
break;
}
}
for (int i = 0; i < is; i++) {
if(number[i] != number[is-1-i]) {
result = false;
}
}
return result;
}
}
- 罗马数字转整数
package com.qianfeng.day03;
import java.util.Scanner;
public class Test03 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String string = input.next();
int number = 0;
number = romanToInt(string);
System.out.println(number);
input.close();
}
public static int romanToInt(String s) {
int number = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == 'M') {
number += 1000;
} else if (s.charAt(i) == 'D') {
number += 500;
} else if (s.charAt(i) == 'L') {
number += 50;
} else if (s.charAt(i) == 'V') {
number += 5;
} else if (s.charAt(i) == 'C') {
if(i != s.length()-1) {
if (s.charAt(i + 1) == 'D' || s.charAt(i + 1) == 'M') {
number -= 100;
} else {
number += 100;
}
} else {
number +=100;
}
} else if (s.charAt(i) == 'X') {
if(i != s.length()-1) {
if (s.charAt(i + 1) == 'L' || s.charAt(i + 1) == 'C') {
number -= 10;
} else {
number += 10;
}
} else {
number +=10;
}
} else if (s.charAt(i) == 'I') {
if(i != s.length()-1) {
if (s.charAt(i + 1) == 'V' || s.charAt(i + 1) == 'X') {
number -= 1;
} else {
number += 1;
}
} else {
number +=1;
}
}
}
return number;
}
}
|