对于以下代码
struct orderSlip
{
char source[64];
char destination[64];
char item[64];
int position;
};
//global
struct orderSlip data[100];
Run Code Online (Sandbox Code Playgroud)
除了以下方法之外,还有其他方法可以打印出每个元素的数据:
printf("%s\n",data[0].source);
printf("%s\n",data[0].destination);
printf("%s\n",data[0].item);
printf("%i\n\n", data[0].position);
printf("%s\n",data[1].source);
printf("%s\n",data[1].destination);
printf("%s\n",data[1].item);
printf("%i\n", data[1].position);
Run Code Online (Sandbox Code Playgroud)
等等
for(int n = 0; n< 3; n++)
{
printf("%s\n",data[n].source);
printf("%s\n",data[n].destination);
printf("%s\n",data[n].item);
printf("%i\n\n", data[n].position);
}
Run Code Online (Sandbox Code Playgroud)
要删除和添加,我是否必须创建一个动态的结构数组?如果是这样,最简单的语法是什么?像这个c ++代码的东西
int * bobby;
bobby = new int [5];
delete bobby[5];
Run Code Online (Sandbox Code Playgroud)
但在C?我猜它与malloc和free有关
我一直在寻找Netbeans的选项,但我找不到改变大括号的默认位置的方法
public class foo {
}
Run Code Online (Sandbox Code Playgroud)
对此
public class foo
{
}
Run Code Online (Sandbox Code Playgroud) Icon icon = new ImageIcon(getClass().getResource( "/img/icon.gif" ) );
aButton = new JButton("Its a button", icon);
Run Code Online (Sandbox Code Playgroud)
是否有某种方法可以阻止动画播放?我正在考虑分配gif的静态jpg,然后当我悬停时,分配动画gif,但我不认为有一个事件可以取消鼠标,MouseMotionListener
所以我可以加载静态jpg.
按钮中的gif循环,但是,如果我将鼠标悬停在按钮上,它就会消失.
如果我的鼠标光标不在按钮上,如何使gif静态?
如果我使用MouseMotionListener
,如果我取下鼠标,它是否会触发事件?
@Override
public void mouseMoved(MouseEvent e) {
//play the gif
//if I take mouse off, call some method to stop playing animated gif
}
@Override
public void mouseDragged(MouseEvent e) {
}
Run Code Online (Sandbox Code Playgroud) 我可以使用toString逐行打印到控制台,但是当我把它放在文本文件中时怎么会这样做呢?
public class NewClass
{
@Override
public String toString()
{
return ("John " + "\n" + "jumps " + "\n" + "fences");
}
}
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class Sandbox
{
public static void main(String[] args) throws IOException
{
NewClass object = new NewClass();
FileWriter file = new FileWriter("output.txt");
PrintWriter output = new PrintWriter(file);
output.println(object.toString());
output.close();
System.out.println(object.toString());
}
}
Run Code Online (Sandbox Code Playgroud)
控制台输出:
约翰
跳跃
围栏
output.txt的
约翰跳过篱笆
我有一个名为Foo的类,它扩展了一个名为Bar的类,它扩展了JPanel并实现了ActionListener.当我选择圆形并单击绘制按钮时,我绘制一个圆形,当我按下矩形并单击绘制时,它会删除前一个形状并绘制一个矩形.
但是,我想在JPanel上保留所有形状,直到我选择单击"删除"按钮.所以我删除了super.paintComponent(g)
它并且它可以工作,但它也导致类Bar的按钮以一种小故障的方式重新出现.如何再次停止绘画按钮?我想不要扩展Bar并让Foo扩展JPanel.
public class Bar extends JPanel implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand() == "Draw")
{
this.requestDraw = true;
repaint();
}
if (e.getActionCommand() == "Circle")
{
requestRectangle = false;
requestTriangle = false;
requestCircle = true;
}
if (e.getActionCommand() == "Rectangle")
{
requestCircle = false;
requestTriangle = false;
requestRectangle = true;
}
if (e.getActionCommand() == "Right Triangle")
{
requestCircle = false;
requestRectangle = false;
requestTriangle = true;
}
}
public class Foo extends Bar
{ …
Run Code Online (Sandbox Code Playgroud) 我正在尝试交换一个结构数组,我认为遵循类似的方式存储在临时文件中会像这样工作:
int temp ,a, b;
temp = a;
a = b;
b = temp;
Run Code Online (Sandbox Code Playgroud)
我的结构定义数组是这样的:
struct storage data[10];
Run Code Online (Sandbox Code Playgroud)
我试图交换一组结构,我试过这个:
struct storage temp[1];
temp = data[1];
data[1] = data[2];
data[2] = temp;
Run Code Online (Sandbox Code Playgroud)
不幸的是,它没有编译
我的错误如下:
错误#2168:'='的操作数具有不兼容类型的struct storage [1]'和'struct storage'.
错误#2088:需要左值.
错误#2168:'='的操作数具有不兼容类型'struct storage'和'struct storage*'.
我有一个名为Bar的主类调用类Foo,我认为我正确地放入了一个深层构造函数
深拷贝构造函数的目的是将一个对象的内容复制到另一个对象,并且更改复制的对象不应该更改原始内容,对吗?
我的代码那样做,但我不明白为什么当我设置原始对象变量时,复制对象不包含该set变量,它只包含默认的构造函数变量.
public class Bar
{
public static void main(String[] args)
{
Foo object = new Foo();
object.setName1("qwertyuiop");
//the below line of code should copy object to object2?
Foo object2 = new Foo(object);
System.out.println(object.getName1());
//shouldn't the below line of code should output qwertyuiop since object2 is a copy of object? Why is it outputting the default constructor value Hello World?
System.out.println(object2.getName1());
//changes object2's name1 var to test if it changed object's var. it didn't, so my deep copy constructor …
Run Code Online (Sandbox Code Playgroud)