integer

Get integer from Textbox

自作多情 提交于 2019-12-28 18:13:52
问题 I am very new to C# and this question might sound very stupid. I wonder how I'm going get the integer(user's input) from the textBox1 and use it in if else statement? Please give some examples 回答1: You need to parse the value of textbox.Text which is a string to int value. You may use int.TryParse, or int.Parse or Convert.ToInt32. TextBox.Text property is of string type. You may look at the following sample code. int.TryParse This will return true if the parsing is successful and false if it

convert string[] to int[]

僤鯓⒐⒋嵵緔 提交于 2019-12-28 06:45:28
问题 Which is the fastest method for convert an string's array ["1","2","3"] in a int's array [1,2,3] in c#? thanks 回答1: var values = new string[] { "1", "2", "3" }; values.Select(x => Int32.Parse(x)).ToArray(); 回答2: string[] arr1 = {"1","2","3"}; int[] arr2 = Array.ConvertAll(arr1, s => int.Parse(s)); The use of Array.ConvertAll ensures (unlike LINQ Select / ToArray ) that the array is initialized at the right size. You can possible get a shade quicker by unrolling, but not much: int[] arr2 = new

convert string[] to int[]

淺唱寂寞╮ 提交于 2019-12-28 06:45:27
问题 Which is the fastest method for convert an string's array ["1","2","3"] in a int's array [1,2,3] in c#? thanks 回答1: var values = new string[] { "1", "2", "3" }; values.Select(x => Int32.Parse(x)).ToArray(); 回答2: string[] arr1 = {"1","2","3"}; int[] arr2 = Array.ConvertAll(arr1, s => int.Parse(s)); The use of Array.ConvertAll ensures (unlike LINQ Select / ToArray ) that the array is initialized at the right size. You can possible get a shade quicker by unrolling, but not much: int[] arr2 = new

convert string[] to int[]

半城伤御伤魂 提交于 2019-12-28 06:45:04
问题 Which is the fastest method for convert an string's array ["1","2","3"] in a int's array [1,2,3] in c#? thanks 回答1: var values = new string[] { "1", "2", "3" }; values.Select(x => Int32.Parse(x)).ToArray(); 回答2: string[] arr1 = {"1","2","3"}; int[] arr2 = Array.ConvertAll(arr1, s => int.Parse(s)); The use of Array.ConvertAll ensures (unlike LINQ Select / ToArray ) that the array is initialized at the right size. You can possible get a shade quicker by unrolling, but not much: int[] arr2 = new

Algorithm for finding the ratio of two floating-point numbers?

℡╲_俬逩灬. 提交于 2019-12-28 06:19:08
问题 I need to find the ratio of one floating-point number to another, and the ratio needs to be two integers. For example: input: 1.5, 3.25 output: "6:13" Does anyone know of one? Searching the internet, I found no such algorithm, nor an algorithm for the least common multiple or denominator of two floating-point numbers (just integers). Java implementation: This is the final implementation that I will use: public class RatioTest { public static String getRatio(double d1, double d2)//1.5, 3.25 {

Convert a string containing a hexadecimal value starting with “0x” to an integer or long

梦想的初衷 提交于 2019-12-28 05:58:07
问题 How can I convert a string value like "0x310530" to an integer value in C#? I've tried int.TryParse (and even int.TryParse with System.Globalization.NumberStyles.Any ) but it does not work. UPDATE: It seems that Convert.ToInt64 or Convert.ToInt32 work without having to remove the trailing "0x": long hwnd = Convert.ToInt64("0x310530", 16); The documentation of Convert.ToInt64 Method (String, Int32) says: "If fromBase is 16, you can prefix the number specified by the value parameter with "0x"

Simplest way to check if two integers have same sign?

跟風遠走 提交于 2019-12-28 04:53:05
问题 Which is the simplest way to check if two integers have same sign? Is there any short bitwise trick to do this? 回答1: Here is a version that works in C/C++ that doesn't rely on integer sizes or have the overflow problem (i.e. x*y>=0 doesn't work) bool SameSign(int x, int y) { return (x >= 0) ^ (y < 0); } Of course, you can geek out and template: template <typename valueType> bool SameSign(typename valueType x, typename valueType y) { return (x >= 0) ^ (y < 0); } Note: Since we are using

Why int j = 012 giving output 10?

 ̄綄美尐妖づ 提交于 2019-12-28 04:24:05
问题 In my actual project It happened accidentally here is my modified small program. I can't figure out why it is giving output 10 ? public class Int { public static void main(String args[]) { int j=012;//accidentaly i put zero System.out.println(j);// prints 10?? } } After that, I put two zeros still giving output 10. Then I change 012 to 0123 and now it is giving output 83? Can anyone explain why? 回答1: Than I change 012 to 0123 and now it is giving output 83? Because, it's taken as octal base

Function To Create Regex Matching a Number Range

[亡魂溺海] 提交于 2019-12-28 04:04:41
问题 I am working with the Amazon Mechanical Turk API and it will only allow me to use regular expressions to filter a field of data. I would like to input an integer range to a function, such as 256-311 or 45-1233, and return a regex that would match only that range. A regex matching 256-321 would be: \b((25[6-9])|(2[6-9][0-9])|(3[0-1][0-9])|(32[0-1]))\b That part is fairly easy, but I am having trouble with the loop to create this regex. I am trying to build a function defined like this:

Error handling using integers as input

我的未来我决定 提交于 2019-12-28 03:14:06
问题 Ive set up this program that checks the mark out of 100 for a test. If the user inputs less than 60 it should say fail if more than 59, pass. mark = int(input("Please enter the exam mark out of 100 ")) if mark < 60: print("\nFail") elif mark < 101: print("\nPass") else: print("\nThe mark is out of range") how do i get the program not to have errors if the user does not input the Integer. Please help, is there a quick solution that 14 year olds would understand? 回答1: Save the input in a