三个初级java编程问题,求简易解答
发布网友
发布时间:2024-04-09 21:08
我来回答
共6个回答
热心网友
时间:2024-04-10 00:04
import java.util.Scanner;
/**
* <p>description:验证输入数据是否排序</p>
* */
public class Ljq0929Demo1 {
public static boolean isSorted(int[] list){
int i = list.length;
boolean isSorted = false;
for(int k = 0;k < i-1;k++){
if(list[k]<list[k+1]){
isSorted = true;
}else{
isSorted = false;
break;
}
}
if(isSorted == true){
System.out.println("The list is already sorted");
}else{
System.out.println("The list is not sorted");
}
return isSorted;
}
public static void main(String args[]){
Scanner in = new Scanner(System.in);
System.out.print("Enter list:");
int n = in.nextInt();
int list[] = new int[n];
for(int i=0;i<n;i++){
list[i] = in.nextInt();
}
isSorted(list);
}
}
第一题的
package text.sort;
import java.util.Scanner;
public class Ljq0930Demo1 {
public static int[] merge(int[] list1,int[] list2){
int len_A = list1.length;
int len_B = list2.length;
int length = len_A + len_B;
int list[] = new int[length];
for(int i = 0;i < len_A;i++){
list[i] = list1[i];
}
for(int j = 0;j < len_B;j++){
list[len_A+j] = list2[j];
}
//冒泡排序
System.out.print("The merged list is:");
for(int k = length-1;k > 0;k--){
for( int i = 0;i < k;i++){
if(list[i] > list[i+1]){
int temp = list[i];
list[i] = list[i+1];
list[i+1] = temp;
}
}
}
for(int n = 0;n < length;n++){
System.out.print(list[n]+" ");
}
return list;
}
public static void main(String[] args){
Scanner in_list1 = new Scanner(System.in);
Scanner in_list2 = new Scanner(System.in);
System.out.print("Enter list1:");
int n = in_list1.nextInt();
System.out.print("Enter list2:");
int m = in_list2.nextInt();
int list1[] = new int[n];
int list2[] = new int[m];
for(int i = 0;i < n;i++){
list1[i] = in_list1.nextInt();
}
for(int i = 0;i < m;i++){
list2[i] = in_list2.nextInt();
}
merge(list1,list2);
}
}
第二题的
package text.sort;
import java.util.Scanner;
public class Ljq0930Demo2 {
static int[][] qipan = new int[3][3];//初始化棋盘,3X3
static int X_Type = 1;//初始化棋子类型,2为O,1为X;
static int O_Type = 2;//初始化棋子类型,2为O,1为X;
static int sequence = 0;//初始化顺序,0为X下子,1为O下子;
static boolean win = false;//初始化是否胜利,0为否,1为是,没有胜利的话就一直下子;
static int Win_Type = 0;//初始化赢的一方的类型,2为O,1为X;
/**时改变棋子的标志,棋子最初标志为空*/
public static String ChangeSign(int n){
String sign = ""; //初始化标志,分别为:" ","X","O"
if(n == 0){
sign = " ";
}else if(n == X_Type){
sign = "X";
}else if(n == O_Type){
sign = "O";
}
return sign;
}
/**显示棋子*/
public static void show(int Type,int x,int y){
if(Type == X_Type){
qipan[x][y] = X_Type;
}else if(Type == O_Type){
qipan[x][y] = O_Type;
}
System.out.println("---------------");
System.out.println(" | " + ChangeSign(qipan[0][0]) + " | " + ChangeSign(qipan[0][1]) + " | " + ChangeSign(qipan[0][2]) + " |");
System.out.println("---------------");
System.out.println(" | " + ChangeSign(qipan[1][0]) + " | " + ChangeSign(qipan[1][1]) + " | " + ChangeSign(qipan[1][2]) + " |");
System.out.println("---------------");
System.out.println(" | " + ChangeSign(qipan[2][0]) + " | " + ChangeSign(qipan[2][1]) + " | " + ChangeSign(qipan[2][2]) + " |");
System.out.println("---------------");
}
/**判断是否获胜*/
public static boolean isWin(){
boolean isWin = false;
if(qipan[0][0] == qipan[1][1] && qipan[1][1] == qipan[2][2] && qipan[0][0] != 0){
isWin = true;
Win_Type = qipan[0][0];
}
if(qipan[0][2] == qipan[1][1] && qipan[1][1] == qipan[2][0] && qipan[0][2] != 0){
isWin = true;
Win_Type = qipan[0][2];
}
else{
for(int i = 0;i < 2;i++){
//如果某一列的值相同,赢
if(qipan[i][0] == qipan[i][1] && qipan[i][1] == qipan[i][2] && qipan[i][0] != 0){
isWin = true;
Win_Type = qipan[i][0];
}
else if(qipan[0][i] == qipan[1][i] && qipan[1][i] == qipan[2][i] && qipan[0][i] != 0){
isWin = true;
Win_Type = qipan[0][i];
}
}
}
if(isWin == true){
System.out.println(ChangeSign(Win_Type)+" player won");
}
return isWin;
}
/**主方法*/
public static void main(String args[]){
Scanner in = new Scanner(System.in);
while(win == false){
if(sequence == 0){
System.out.print("Enter a row(0, 1, or 2) for player X:");
int X_row = in.nextInt();
System.out.print("Enter a colum (0, 1, or 2)for palyer X:");
int X_col = in.nextInt();
show(X_Type,X_row,X_col);
win = isWin();
sequence = 1;
}else if(sequence == 1){
System.out.print("Enter a row(0, 1, or 2) for player O:");
int O_row = in.nextInt();
System.out.print("Enter a colum (0, 1, or 2)for palyer O:");
int O_col = in.nextInt();
show(O_Type,O_row,O_col);
win = isWin();
sequence = 0;
}
}
}
}
第三题的
热心网友
时间:2024-04-10 00:04
第一题。
import java.util.Scanner;
public class test {
public static boolean isSorted(int[] list){
int i=list.length;
boolean count = true;
for(int j=0;j<i-1;j++){
if(list[j]>list[++j]){
count=false;break;
}
}
return count;
}
public static void main(String[] args){
Scanner scan=new Scanner(System.in);
System.out.println("please input the size of the array and the content:");
int n=scan.nextInt();
int[] list1=new int[n];
for(int m=0;m<n;m++){
list1[m]=scan.nextInt();
}
if(isSorted(list1)){
System.out.println("Sorted");
}else{
System.out.println("not Sorted");
}
}
}
热心网友
时间:2024-04-10 00:05
public bool isSort(int[] nums)
{
for (int i = 1; i < nums.Length; i++)
{
if (nums[i] < nums[i - 1])
return false;
}
return true;
}
public int[] merge(int[] list1,int[] list2)
{
int[] merge = new int[list1.Length+list2.Length];
for (int i = 0,j = 0,k = 0; k < merge.Length; k++)
{
if (list1[i] < list2[j])
{
merge[k] = list1[i];
i++;
}
else
{
merge[k] = list2[j];
j++;
}
}
return merge;
}
前两道题
热心网友
时间:2024-04-10 00:05
刚学一个月的题目就这样了啊,不容易啊
热心网友
时间:2024-04-10 00:06
我怎么给你发过去 三道题的答案都在附件中,你自己调试一下,前两题,你参照楼上的吧。
热心网友
时间:2024-04-10 00:07
哪个大学?