这是以相反顺序打印整数数组的相当简单的问题.虽然每当我尝试打印时,它最终都会显示垃圾值.以下是我的计划.
#include <stdio.h>
#include <conio.h>
int main()
{
int temp = { '\0' };
int num[9];
int i;
int j = 8;
printf("Enter 8 numbers\n");
for (i = 0; i < 8; i++)
{
scanf_s("%d", &num[i], 1);
}
for (i = 0; i <= j; i++, j--)
{
temp = num[i];
num[i] = num[j];
num[j] = temp;
}
printf("\nThe numbers in reverse are\n");
for (i = 0; i <=8; i++)
{
printf("%d\n", num[i]);
}
_getch();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
假设我输入的数字从1到8,它会反向打印数字,但它打印的第一个值是垃圾值.我知道我可以使用和If语句来对抗这种情况,但有没有办法在不使用if的情况下解决这个问题?
如何在richtextbox中只更新一行文本?
String[] lines = richTextBox8.Lines;
lines[2] += " ";
richTextBox8.Lines = lines;
Run Code Online (Sandbox Code Playgroud)
我正在使用此代码部分来更新 Richtextbox 的第二行,但它会扫描我所有的 Richtextbox 行并且需要很多次。
所以我想更新 1 行的行文本。
我怎样才能做到这一点?
我有一个列表,我想用LINQ更新.
class Student
{
private string name;
private int marks;
public string Name { get; set;}
public int Marks { get; set; }
public Student(string name, int marks)
{
Name = name;
Marks = marks;
}
}
List<Student> myList = new List<Student>();
myList.Add(new Student("John", 10));
myList.Add(new Student("Tom", 20));
Run Code Online (Sandbox Code Playgroud)
现在我想使用LINQ更新列表,以便只更新John的标记.我使用以下语法:
myList.Where(w => w.Name == "Tom").Select(w=> { w.Marks = 35; return w});
Run Code Online (Sandbox Code Playgroud)
但这不会更新myList中的数据.有人能告诉我哪里出错了.
我试图显示以下HashMap的内容:
HashMap<Character,Integer> hm = new HashMap<Character,Integer>();
Run Code Online (Sandbox Code Playgroud)
我使用以下方法打印出内容:
Set hmset = hm.entrySet();
Iterator iterator = hmset.iterator();
while(iterator.hasNext())
{
Character key = new Character(iterator.next());
System.out.println("key : "+key+"value : "+(Integer)hm.get(key));
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
error: constructor Character in class Character cannot be applied to given types;
Run Code Online (Sandbox Code Playgroud)
我还尝试了以下类型转换方式:
Character key = (Character)iterator.next();
Run Code Online (Sandbox Code Playgroud)
但那也行不通.任何帮助非常感谢.谢谢..
我不确定这里发生了什么.任何启蒙都会受到高度赞赏.
ArrayList<Integer> al = new ArrayList<>();
for(int i = 0; i < 10; i++)
al.add(i);
for(Integer i : al)
System.out.println(al.get(i));
al.add(2,8); //should add the value 8 to index 2?
System.out.println();
for(Integer i : al)
System.out.println(al.get(i));
Run Code Online (Sandbox Code Playgroud)
产量
0
1
2
3
4
5
6
7
8
9
0
1
7
8
2
3
4
5
6
7
8
Run Code Online (Sandbox Code Playgroud)
为什么在7和8中加入...... 9在哪里?
目前,如果我定义了一堆不同的字符串,我就是这样做的;
string a = "A";
string b = "B";
string c = "C";
string d = "D";
string e = "E";
Run Code Online (Sandbox Code Playgroud)
有没有更简单的方法在一行中定义所有这些字符串?
我的代码看起来像这样:
public class Hashtabledemo2 {
public static void main(String[] args) {
Hashtable myCompay = new Hashtable(10);
System.out.println("Add some employee objects to the hashtable..");
Salary e1 = new Salary("Salary1", "Palo Alto, CA",
1, 100000.00);
Hourly e2 = new Hourly("Hourly2", "Cupertino, CA", 2, 100.00);
Contractor e3 = new Contractor("Contractor3", "Milpitas, CA",3, 1000.00);
myCompay.put(new Integer(e1.hashCode()), e1);
myCompay.put(new Integer(e2.hashCode()), e2);
myCompay.put(new Integer(e3.hashCode()), e2);
System.out.println("The size of the hashtable is "+myCompay.size());
int size = myCompay.size();
for(int i=1;i<=size;i++){
/* Note that the get() method of the …Run Code Online (Sandbox Code Playgroud) 如果我使用intindex来访问vector元素,它会将整数转换为size_t,然后调用operator[](size_t)函数吗?是否有任何性能下降?
也许这是一个愚蠢的问题。但我想了解它,找不到答案。当我写如下的东西时:
int test[1000000] = {0};
Run Code Online (Sandbox Code Playgroud)
这个数组会包含在编译后的程序代码中吗?还是仅保留该阵列可用内存的指令?
我想了解C ++在这种情况下是在二进制代码中包含所有数组的值还是在运行时分配内存?
我有2个LINQ查询,一个返回我期望的,另一个没有,我试图理解为什么.我试图找出是否从Config中的所有节点,是否有一个名为"TEST"的节点,其Selected属性为True.
查询1 - 返回正确的东西的条件是Any():
var res1 =
(from config in _config.CurrentSettings.Config let name = config.name select config).Any(
config => config.name.Equals("TEST") && config.selected == true);
Run Code Online (Sandbox Code Playgroud)
失败的查询2具有Select中的条件:
(_config.CurrentSettings.Config.Select(config => config.name.Equals("TEST") && config.selected))
.Any();
Run Code Online (Sandbox Code Playgroud)