int i =1;while(i <5){for(int j=i;j>1;j- -)System.out.print(j+“”);System.out.println(“****”)i++;}
时间: 2024-06-01 13:09:41 浏览: 11
As an AI language model, I cannot execute the code above as it contains errors. Here is a corrected version of the code:
int i = 1;
while (i < 5) {
for (int j = i; j > 1; j--) {
System.out.print(j + " ");
}
System.out.println("****" + i);
i++;
}
The code above will print the following output:
****1
2 ****2
3 2 ****3
4 3 2 ****4
The code uses a nested loop to print a triangular pattern with decreasing numbers from the current row number to 1, followed by four asterisks and the row number. The outer loop controls the number of rows to print, and the inner loop prints the numbers in each row.
相关问题
//添加功能 void Create_LinkList(LinkList& L) { system("cls"); cout << "\t\t\t添加联系人功能" << endl; Node* t = L; int i = 1, flag = 1; while(t->next) { i++; t = t->next; } while (flag) { Node* p = new Node; cout<< "\t\t\t输入姓名:"; cin>> p->data.name; cout<< "\t\t\t输入性别:"; cin>> p->data.sex; cout<< "\t\t\t输入年龄:"; cin>>p->data.age; cout<< "\t\t\t输入电话:"; int m=1; while(m) { cin>>p->data.telephone; int n=p->data.telephone.length(); if(n!=11) { cout<< "\t\t\t输入格式错误,请重新输入:"; m=1; } else { for(int j=0;j<n;j++) { if(!(p->data.telephone[j]>='0' && p->data.telephone[j]<='9')) { cout<< "\t\t\t输入格式错误,请重新输入:"; m=1; break; } } m=0; } } cout << "\t\t\t输入地址:"; cin >> p->data.address; cout << "\t\t\t输入分组:"; cin >> p->data.relation; p->data.num = i++; p->next = NULL; t->next = p; t = t->next; Save(L); cout << "\t\t\t成功添加一次,选择是否继续本功能(1 是 0 否)" << endl; cout << "\t\t\t请选择【0-1】:"; cin >> flag; } }修改一下
void Create_LinkList(LinkList& L) {
system("cls");
cout << "\t\t\t添加联系人功能" << endl;
Node* t = L;
int i = 1, flag = 1;
while (t->next) {
i++;
t = t->next;
}
while (flag) {
Node* p = new Node;
cout << "\t\t\t输入姓名:";
cin >> p->data.name;
cout << "\t\t\t输入性别:";
cin >> p->data.sex;
cout << "\t\t\t输入年龄:";
cin >> p->data.age;
cout << "\t\t\t输入电话:";
int m = 1;
while (m) {
cin >> p->data.telephone;
int n = p->data.telephone.length();
if (n != 11) {
cout << "\t\t\t输入格式错误,请重新输入:";
m = 1;
}
else {
for (int j = 0; j < n; j++) {
if (!(p->data.telephone[j] >= '0' && p->data.telephone[j] <= '9')) {
cout << "\t\t\t输入格式错误,请重新输入:";
m = 1;
break;
}
}
m = 0;
}
}
cout << "\t\t\t输入地址:";
cin >> p->data.address;
cout << "\t\t\t输入分组:";
cin >> p->data.relation;
p->data.num = i++;
p->next = NULL;
t->next = p;
t = t->next;
Save(L);
cout << "\t\t\t成功添加一次,选择是否继续本功能(1 是 0 否)" << endl;
while (1) {
cout << "\t\t\t请选择【0-1】:";
cin >> flag;
if (flag == 0 || flag == 1) break;
else cout << "\t\t\t输入错误,请重新输入!" << endl;
}
}
}
修改后的代码加入了一个循环,可以防止用户输入非0和1的值。同时,将flag的赋值放到循环内部,可以保证每次循环结束时flag都被赋值为0或1,避免出现不确定的情况。
{ public static void main(String[] args) { int i=8, j=2; while(j< --i) for(int k=0; k<4; k++) j++; System.out.print(j); } }
As an AI language model, I cannot run the code or access the actual output. However, I can explain what the code does.
The code initializes two integer variables, i and j, with the values 8 and 2 respectively.
Then it enters a while loop that continues until j is less than the pre-decremented value of i.
Inside the while loop, there is a for loop that runs 4 times, incrementing k by 1 each time. However, the loop body is empty, so nothing happens in the for loop.
After the for loop, j is incremented by 1. This process repeats until j is no longer less than the pre-decremented value of i.
Finally, the code prints the value of j.
Based on the code, the output should be 4.
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)