可能重复:
Java中的char和int
AsicII表:http://www.asciitable.com
下面的代码打印出相应字符的Dec值,例如"123" - > 49 50 51
public void foo(String str)
{
for(int x = 0; x < str.length(); x++)
{
int temp = str.charAt(x);
System.out.println(temp);
}
}
Run Code Online (Sandbox Code Playgroud)
但我注意到java是一种强类型语言,这意味着每一件事都必须在编译时进行转换,但是为什么代码知道如何以及何时将char转换为AsicII表中正确的Dec值?我搞乱了任何java /编程基础吗?
我有一个程序从用户接收命令,它将以不同的方式处理不同的命令.例如:
ADD_STUDENT ALEX 5.11 175
ADD_TEACHER MERY 5.4 120 70000
PRINT MERY
REMOVE ALEX
PRINT TEACHER SALARY
PRINTALL
Run Code Online (Sandbox Code Playgroud)
因此,我需要检查每一行,看看输入是由什么组成的.
这是我的代码,但我想我误解了"工作方式".有人可以给我一个建议吗?并告诉我为什么我的代码不能像我预期的那样工作?
string line;
while(getline(cin, line))
{
//some initialization of string, float variable
std::istringstream iss(line);
if(iss >> command >> name >> height >> weight)
..examine the command is correct(ADD_STUDENT) and then do something..
else if(iss >> command >> name >> height >> weight >> salary)
..examine the command is correct(ADD_TEACHER) and then do something...
else if(iss >> command >> name)
..examine the …Run Code Online (Sandbox Code Playgroud) 我是C++的新手.
我有一个类,我想在类的函数中创建一个线程.并且该线程(函数)也将调用并访问类函数和变量.一开始我尝试使用Pthread,但只在类外工作,如果我想访问类函数/变量,我得到了一个超出范围的错误.我看一下Boost/thread但是不可取,因为我不想在我的文件中添加任何其他库(出于其他原因).
我做了一些研究,找不到任何有用的答案.请举一些例子来指导我.非常感谢!
尝试使用pthread(但我不知道如何处理我上面提到的情况):
#include <pthread.h>
void* print(void* data)
{
std::cout << *((std::string*)data) << "\n";
return NULL; // We could return data here if we wanted to
}
int main()
{
std::string message = "Hello, pthreads!";
pthread_t threadHandle;
pthread_create(&threadHandle, NULL, &print, &message);
// Wait for the thread to finish, then exit
pthread_join(threadHandle, NULL);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我阅读了示例代码并尝试了以下内容,但它不起作用。文本似乎不是超链接。请帮忙:(
String randomString = XXXX.getString();
if(randomString .contains("XXXX"))
{
TextView tv = new TextView(this);
tv.setMovementMethod(LinkMovementMethod.getInstance());
tv.setText(randomString +"/n"+Html.fromHtml("<a href=https://play.google.com/store/apps/details?id=com.xxxxxxxxx>Click Here</a>"));
AlertDialog dialog = new AlertDialog.Builder(activity.this)
.setView(tv)
.setPositiveButton("OK!", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
}
})
.show();
}
Run Code Online (Sandbox Code Playgroud)
编辑:这项工作:
tv.setText(Html.fromHtml("<br><a href=https://play.google.com/store/apps/details?id=com.xxxxxxxxx>Click Here</a>"));
Run Code Online (Sandbox Code Playgroud)
没有 randomString+
只要我把 randomString+Html.fromHtml ....“点击这里”变成一个普通文本
但我也想将 randomString 放在 textview 中。
这是我的问题:
class A
{
B mB = new B(); //this is fine
int y = mB.method1(); //this is error;
public void method2()
{
int x = mB.method1(); //this is fine
}
}
Run Code Online (Sandbox Code Playgroud)
有人可以解释上面的差异吗?
我在while循环中使用了scanf().
while(1)
{
int input = 0;
scanf("%d\n", &input);
printf("%d\n", input);
}
Run Code Online (Sandbox Code Playgroud)
当我运行这个程序,并输入一个数字时,printf()除非我再次输入另一个数字,否则不会显示该数字.为什么?
我正在.Net C#中构建应用程序.我不小心双击了我的应用程序中的文本框,并且创建了一个textbox_click函数,我删除了该函数,因为我不需要它,但是当我运行我的应用程序时,有一个:
Error 1 'Bond_Yield_Calculator.BaseForm' does not contain a definition for 'textBox5_TextChanged' and no extension method 'textBox5_TextChanged' accepting a first argument of type 'Bond_Yield_Calculator.BaseForm' could be found (are you missing a using directive or an assembly reference?) C:\Users\Alex Chan\documents\visual studio 2010\Projects\Bond Yield Calculator\Bond Yield Calculator\BaseForm.Designer.cs 139 77 Bond Yield Calculator
Run Code Online (Sandbox Code Playgroud)
错误,我该如何解决这个问题?
我想使用 istream 和 ostream 作为我的函数采用的参数并帮助我完成工作(读取文件,并在屏幕上显示内容)。我知道如何用 istream 做到这一点:
std::ifstream myfile(...);
void foo(myfile);
void readFoo(std::istream &stream)
{
int x, y;
stream >> x >> y; //suppose my file contains many rows of 2 numbers.
//store the x and y to somewhere
}
void writeFoo(std::ostream &output)
{
???
}
Run Code Online (Sandbox Code Playgroud)
我应该将什么对象传递给我的 writeFoo()?
更新:这是更新,但我收到一条错误消息(无法从 ostream 转换为 ostream*)
writeFoo(std::cout);
writeFoo(sd::ostream &out)
{
out << somedata to display to the screen;
}
Run Code Online (Sandbox Code Playgroud) 我意识到,当你这样做时const auto_ptr<X> ptr = variable,你仍然可以修改auto_ptr指向的变量的内容.对于原始指针const X * ptr = variable,const会阻止您修改它.
那么在auto_ptr前面使用const的目的究竟是什么?
// Example program
#include <iostream>
#include <string>
struct foo
{
int d_member;
foo(const int & in):
d_member(in){};
};
int main()
{
foo *p;
{
int temp = 5;
p = new foo(temp);
}
std::cout << p->d_member << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Foo将一个const引用它的ctor转换为一个int,然后我将一个临时的int传递给它.温度在新的之后被摧毁.为什么我仍然可以打印出d_member?