if ( x1>x2 & x2>x3 & x3>x4 & .........& x99>x100}
这种有规律的条件 并集 怎么简写?
这种有规律的条件 并集 怎么简写?
这种有规律的条件 并集 怎么简写?
用数组+循环
boolean result = true;
int[] arr = { x1, x2, x3, ... , x100 };
for (int i = 0; i < 99; i++)
{
if (arr[i] <= arr[i + 1]) { result = false; break; }
}
不知道你这个问题是否已经解决, 如果还没有解决的话:public static void main(String[] args) {
login();
}
public static void login() {
Scanner input = new Scanner(System.in);
String[][] bookInfor = initi().clone();
while (true) {
System.out
.println("-----------------------------------------------");
System.out
.println("1:新增图书 2、查看图书 3、删除图书 4、借出图书 5、归还图书 0、退出图书系统");
System.out
.println("-----------------------------------------------");
System.out.println("请输入您需要操作的序号:");
int no = input.nextInt();
if (no == 0) {
break;
}
switch (no) {
case 1:
addBook(bookInfor);
break;
case 2:
checkBook(bookInfor);
break;
case 3:
deleteBook(bookInfor);
break;
case 4:
lendBook(bookInfor);
break;
case 5:
returnBook(bookInfor);
break;
default:
System.out.println("输入有误!请重新输入:");
break;
}
}
}
// 初始化操作
public static String[][] initi() {
Date date = new Date();
String[][] bookInformation = new String[6][4];
bookInformation[0][0] = "语文";
bookInformation[0][1] = "可借";
bookInformation[0][2] = date.toString();
bookInformation[0][3] = "0";
bookInformation[1][0] = "数学";
bookInformation[1][1] = "可借";
bookInformation[1][2] = date.toString();
bookInformation[1][3] = "0";
for (int i = 2; i < bookInformation.length; i++) {
for (int j = 0; j < bookInformation[i].length; j++) {
bookInformation[i][j] = null;
}
}
return bookInformation;
}
// 查看书本
public static void checkBook(String[][] bookInfor) {
System.out.println("书名\t\t\t\t是否可借\t\t\t\t借出日期\t\t\t\t\t\t\t借出次数");
for (int i = 0; i < bookInfor.length; i++) {
if (bookInfor[i][0]!=null) {
for (int j = 0; j < bookInfor[i].length; j++) {
System.out.print(bookInfor[i][j] + "\t\t\t\t");
}
System.out.println();
} else {
continue;
}
}
}
// 添加书本
public static void addBook(String[][] bookInfor) {
Date date = new Date();
Scanner input = new Scanner(System.in);
int count = 0;
for (int j = 0; j < bookInfor.length; j++) {
if (bookInfor[j][0]!=null) {
count++;
}
}
if(count==6){
System.out.println("书本数已经添加满了。");
return ;
}else {
for (int i = 0; i < bookInfor.length; i++) {
if (bookInfor[i][0] == null) {
System.out.println("请输入书名:");
bookInfor[i][0] = input.next();
bookInfor[i][1] = "可借";
bookInfor[i][2] = date.toString();
bookInfor[i][3] = "0";
break;
}
}
}
}
// 删除书本
public static String[][] deleteBook(String[][] bookInfor) {
Scanner input = new Scanner(System.in);
System.out.println("请输入您需要删除的书名:");
String bookName = input.next();
int count = 0;
for (int i = 0; i < bookInfor.length; i++) {
if (bookInfor[i][0] != null) {
if (bookInfor[i][0].equalsIgnoreCase(bookName)) {
for (int j = 0; j < bookInfor[i].length; j++) {
bookInfor[i][j] =null;
count++;
}
}
}
}
if(count>0){
System.out.println("删除图书完成!");
}else {
System.out.println("未找到您想要删除的图书");
}
return bookInfor;
}
//借出图书
public static String[][] lendBook(String[][] bookInfor){
Scanner input = new Scanner(System.in);
int count = 0;
for (int i = 0; i < bookInfor.length; i++) {
if (bookInfor[i][0]!=null) {
System.out.println(bookInfor[i][0]+"\t"+bookInfor[i][1]);
}
}
System.out.println("请输入您要借的书名:");
String bookNameString = input.next();
for (int i = 0; i < bookInfor.length; i++) {
if(bookInfor[i][0]!=null){
if (bookInfor[i][0].equalsIgnoreCase(bookNameString)) {
if(bookInfor[i][3].hashCode()==(48+6)){//判断借书次数是否为6
System.out.println("不好意思,该图书已借完!");
count++;
break;
}else{
Date date = new Date();
bookInfor[i][2] = date.toString();
int num =(bookInfor[i][3].hashCode()+1);
char ch = (char)num;
bookInfor[i][3] = ""+ch;
count++;
}
}
}
}
if (count>0) {
System.out.println("借出图书操作完成!");
}else {
System.out.println("未找到您想借阅的图书!");
}
return bookInfor;
}
//归还图书
public static String[][] returnBook(String[][] bookInfor){
Scanner input = new Scanner(System.in);
System.out.println("请输入您要归还的书名:");
String bookNameString = input.next();
for (int i = 0; i < bookInfor.length; i++) {
if(bookInfor[i][0]!=null){
if (bookInfor[i][0].equalsIgnoreCase(bookNameString)) {
if (bookInfor[i][3].hashCode()<49) {
System.out.println("该书没有借出!");
break;
}else {
int num =(bookInfor[i][3].hashCode()-1);
char ch = (char)num;
bookInfor[i][3] = ""+ch;
System.out.println("归还图书操作完成!");
}
}
}
}
return bookInfor;
}
在Java中,可以使用三目运算符来简写if语句。三目运算符的基本格式是:condition ? expression1 : expression2,如果condition为true,则执行expression1,否则执行expression2。
以下是一个示例代码,演示如何使用三目运算符简写if语句:
import java.util.Scanner;
public class Demo3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
int max = (a > b && a > c) ? a : (b > c) ? b : c;
System.out.println("最大值为" + max);
sc.close();
}
}
在这个示例中,首先使用Scanner类获取用户输入的三个整数 a、b、c。然后,使用三目运算符构造一个表达式根据条件选择最大的数值,并将结果赋值给变量max。最后,使用System.out.println输出最大值。
可以看到,通过使用三目运算符,我们可以更简洁地编写有规律的条件并集的判断,避免了使用一系列的if else if语句的冗长和重复。